javascript 如何从 ArrayBuffer 获取二进制字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16363419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get binary string from ArrayBuffer?
提问by Orest
What is the way to obtain binary string from ArrayBuffer in JavaScript?
在 JavaScript 中从 ArrayBuffer 获取二进制字符串的方法是什么?
I don't want to encode the bytes, just get the binary representation as String.
我不想对字节进行编码,只需将二进制表示作为字符串。
Thanks in advance!
提前致谢!
回答by potatosalad
The following code will consistently convert an ArrayBuffer
to a String
and back again without losing or adding any additional bytes.
以下代码将始终如一地将ArrayBuffer
a转换为 aString
并再次转换回来,而不会丢失或添加任何额外的字节。
function ArrayBufferToString(buffer) {
return BinaryToString(String.fromCharCode.apply(null, Array.prototype.slice.apply(new Uint8Array(buffer))));
}
function StringToArrayBuffer(string) {
return StringToUint8Array(string).buffer;
}
function BinaryToString(binary) {
var error;
try {
return decodeURIComponent(escape(binary));
} catch (_error) {
error = _error;
if (error instanceof URIError) {
return binary;
} else {
throw error;
}
}
}
function StringToBinary(string) {
var chars, code, i, isUCS2, len, _i;
len = string.length;
chars = [];
isUCS2 = false;
for (i = _i = 0; 0 <= len ? _i < len : _i > len; i = 0 <= len ? ++_i : --_i) {
code = String.prototype.charCodeAt.call(string, i);
if (code > 255) {
isUCS2 = true;
chars = null;
break;
} else {
chars.push(code);
}
}
if (isUCS2 === true) {
return unescape(encodeURIComponent(string));
} else {
return String.fromCharCode.apply(null, Array.prototype.slice.apply(chars));
}
}
function StringToUint8Array(string) {
var binary, binLen, buffer, chars, i, _i;
binary = StringToBinary(string);
binLen = binary.length;
buffer = new ArrayBuffer(binLen);
chars = new Uint8Array(buffer);
for (i = _i = 0; 0 <= binLen ? _i < binLen : _i > binLen; i = 0 <= binLen ? ++_i : --_i) {
chars[i] = String.prototype.charCodeAt.call(binary, i);
}
return chars;
}
I tested it by round-tripping the following values in this jsfiddle: http://jsfiddle.net/potatosalad/jrdLV/
我通过在这个 jsfiddle 中来回传递以下值来测试它:http: //jsfiddle.net/potatosalad/jrdLV/
(String) "abc" -> (ArrayBuffer) -> (String) "abc"
(String) "aΩc" -> (ArrayBuffer) -> (String) "aΩc"
(Uint8Array) [0,1,255] -> (ArrayBuffer) -> (String) -> (Uint8Array) [0,1,255]
(Uint16Array) [0,1,256,65535] -> (ArrayBuffer) -> (String) -> (Uint16Array) [0,1,256,65535]
(Uint32Array) [0,1,256,65536,4294967295] -> (ArrayBuffer) -> (String) -> (Uint32Array) [0,1,256,65536,4294967295]
回答by Xotic750
This will give you a binary string from a typed array
这将为您提供来自类型化数组的二进制字符串
var bitsPerByte = 8;
var array = new Uint8Array([0, 50, 100, 170, 200, 255]);
var string = "";
function repeat(str, num) {
if (str.length === 0 || num <= 1) {
if (num === 1) {
return str;
}
return '';
}
var result = '',
pattern = str;
while (num > 0) {
if (num & 1) {
result += pattern;
}
num >>= 1;
pattern += pattern;
}
return result;
}
function lpad(obj, str, num) {
return repeat(str, num - obj.length) + obj;
}
Array.prototype.forEach.call(array, function (element) {
string += lpad(element.toString(2), "0", bitsPerByte);
});
console.log(string);
Output is
输出是
000000000011001001100100101010101100100011111111
On jsfiddle
Or perhaps you are asking about this?
或者也许你在问这个?
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));
}
Note: that using apply
in this manner means that you can hit the argument limitation (some 16000 elements or so), and then you will have to loop through the array elements instead.
注意:apply
以这种方式使用意味着您可以达到参数限制(大约 16000 个元素左右),然后您将不得不循环遍历数组元素。
On html5rocks
回答by M Sach
function string2Bin(s) {
var b = new Array();
var last = s.length;
for (var i = 0; i < last; i++) {
var d = s.charCodeAt(i);
if (d < 128)
b[i] = dec2Bin(d);
else {
var c = s.charAt(i);
alert(c + ' is NOT an ASCII character');
b[i] = -1;
}
}
return b;
}
function dec2Bin(d) {
var b = '';
for (var i = 0; i < 8; i++) {
b = (d%2) + b;
d = Math.floor(d/2);
}
return b;
}