arraybuffer

Create ArrayBuffer from Array (holding integers) and back again

老子叫甜甜 提交于 2019-12-04 10:43:10
问题 It seems so simple, but I cannot find out how to convert an Array filled with integers to an ArrayBuffer and back again to an Array . There are lots of examples where strings are converted to an ArrayBuffer like for example here . Using these examples I created this: /** * Convert string to array buffer. * * @param {Array.<int>} array * @returns {ArrayBuffer} */ self.arrayToArrayBuffer = function( array ) { var length = array.length; var buffer = new ArrayBuffer( length * 2 ); var view = new

Javascript - Converting between Unicode string and ArrayBuffer

痴心易碎 提交于 2019-12-04 09:37:50
Does somebody know a script that is able to convert a string to a ArrayBuffer using unicode encoding? I´m creating a browser-side eqivalent of the "Buffer" of node.js. The only encoding that is left is unicode. All others are done. Thanks for your help! I found it out by myself. Decoding: var b = new Uint8Array(str.length*2); for(var i = 0; i < b.length; i+=2){ var x = str.charCodeAt(i/2); var a = x%256; x -= a; x /= 256; b[i] = x; b[i+1] = a; } Encoding var s = ""; for(var i = 0; i < this.length;){ s += String.fromCharCode(this[i++]*256+this[i++]); } 来源: https://stackoverflow.com/questions

第十章 Scala 容器(三):使用可变与不可变容器特有方法

别等时光非礼了梦想. 提交于 2019-12-04 06:50:28
1. 可变容器通用方法(Common operators (methods) on mutable collections) c += x:把x元素添加到集合c中 scala> val a = collection.mutable.ArrayBuffer(1,2,3) a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3) scala> a += 4 res15: a.type = ArrayBuffer(1, 2, 3, 4) c += (x,y,z):把x,y,z添加到集合c中 scala> val a = collection.mutable.ArrayBuffer(1,2,3) a: scala.collection.mutable.ArrayBuffer[Int] = ArrayBuffer(1, 2, 3) scala> a += (4,5,6) res16: a.type = ArrayBuffer(1, 2, 3, 4, 5, 6) c1 ++= c2:把集合c2的元素全部添加到集合c1中 scala> val c1 = mutable.ArrayBuffer(1,2,3) c1: scala.collection.mutable.ArrayBuffer[Int] =

Trim or cut audio recorded with mediarecorder JS

ぃ、小莉子 提交于 2019-12-03 17:26:47
Requested Knowledge How to shorten (from the front) an array of audio blobs and still have playable audio. Goal I am ultimately trying to record a continuous 45 second loop of audio using the JS MediaRecorder API. The user will be able to push a button and the last 45s of audio will be saved. I can record, playback, and download a single recording just fine. Issue When I have an array called chunks of say 1000 blobs from the MediaRecorder and use chunks.slice(500, 1000) the resulting blob array can't be used to playback or download audio. Oddly enough chunks.slice(0,500) still works fine. Code

Receiving WebSocket ArrayBuffer data in the browser - receiving string instead

偶尔善良 提交于 2019-12-03 14:11:35
I have a node.js server application and a browser client. Sending ArrayBuffer data browser -> server works perfectly, but server -> browser results in a string "[object ArrayBuffer]" being received. This happens in the latest versions of both Chrome and Firefox. Server: var serverPort = 9867; // dependencies var webSocketServer = require('websocket').server; var http = require('http'); var players = {}; var nextPlayerId = 0; // create http server var server = http.createServer(function(request, response) { }); server.listen(serverPort, function() { console.log((new Date()) + " Server is

Appending ArrayBuffers

不羁岁月 提交于 2019-12-03 11:35:37
问题 What is the preferable way of appending/combining ArrayBuffers? I'm receiving and parsing network packets with a variety of data structures. Incoming messages are read into ArrayBuffers. If a partial packet arrives I need to store it and wait for the next message before re-attempting to parse it. Currently I'm doing something like this: function appendBuffer( buffer1, buffer2 ) { var tmp = new Uint8Array( buffer1.byteLength + buffer2.byteLength ); tmp.set( new Uint8Array( buffer1 ), 0 ); tmp

ArrayBuffer vs Blob and XHR2

◇◆丶佛笑我妖孽 提交于 2019-12-03 08:43:38
问题 XHR2 differences states The ability to transfer ArrayBuffer, Blob, File and FormData objects. What are the differences between ArrayBuffer and Blob ? Why should I care about being able to send them over XHR2 ? (I can understand value of File and FormData) 回答1: This is an effort to replace the old method which would take a "string" and cut sections of it out. You would use an ArrayBuffer when you need a typed array because you intend to work with the data, and a blob when you just need the

Create ArrayBuffer from Array (holding integers) and back again

十年热恋 提交于 2019-12-03 06:00:35
It seems so simple, but I cannot find out how to convert an Array filled with integers to an ArrayBuffer and back again to an Array . There are lots of examples where strings are converted to an ArrayBuffer like for example here . Using these examples I created this: /** * Convert string to array buffer. * * @param {Array.<int>} array * @returns {ArrayBuffer} */ self.arrayToArrayBuffer = function( array ) { var length = array.length; var buffer = new ArrayBuffer( length * 2 ); var view = new Uint16Array(buffer); for ( var i = 0; i < length; i++) { view[i] = array[i]; } return buffer; } Then

Where to use ArrayBuffer vs typed array in JavaScript?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 02:48:42
问题 I am moving from Node.js to browser environment, and I am still confused over ArrayBuffer vs. typed arrays (such as Uint8Array). I am confused over where to use the typed arrays, and where to use ArrayBuffer directly. It's not hard to convert one to the other and vice versa, but which to use when? For example, when I am creating an object that will represent a chunk of data in my code, should it be ArrayBuffer or Uint8Array? What does it depend on? Or: should I rather return ArrayBuffer from

ArrayBuffer to base64 encoded string

匿名 (未验证) 提交于 2019-12-03 02:11:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I need an efficient (read native) way to convert an ArrayBuffer to a base64 string which needs to be used on a multipart post. 回答1: function _arrayBufferToBase64 ( buffer ) { var binary = '' ; var bytes = new Uint8Array ( buffer ); var len = bytes . byteLength ; for ( var i = 0 ; i but, non-native implementations are faster e.g. https://gist.github.com/958841 see http://jsperf.com/encoding-xhr-image-data/6 回答2: This works fine for me: var base64String = btoa ( String . fromCharCode . apply ( null , new Uint8Array ( arrayBuffer )));