JavaScript- 将数组缓冲区转换为字符串
JavaScript- convert array buffer to string
标题说明了一切。
我有一个 jquery 序列化数据,如下所示:
tarid=value&tarname=value&sel=3
我想把它转换成ArrayBuffer。
之后,我还需要将它重新变回原来的样子。
那我该怎么做呢?
够了吗?
function stringToArrayBuffer(str){
if(/[\u0080-\uffff]/.test(str)){
throw new Error("this needs encoding, like UTF-8");
}
var arr = new Uint8Array(str.length);
for(var i=str.length; i--; )
arr[i] = str.charCodeAt(i);
return arr.buffer;
}
function arrayBufferToString(buffer){
var arr = new Uint8Array(buffer);
var str = String.fromCharCode.apply(String, arr);
if(/[\u0080-\uffff]/.test(str)){
throw new Error("this string seems to contain (still encoded) multibytes");
}
return str;
}
或者您需要真正的 UTF-8 编码
编辑:完整的 UTF-8 支持
Beware/Disclaimer: 此代码未针对 UTF-8 编码器或解码器的某些外国实现进行测试。它可能会产生错误的结果。
在生产中使用之前,请自行测试!
function stringToArrayBuffer(str){
if(/[\u0080-\uffff]/.test(str)){
var arr = new Array(str.length);
for(var i=0, j=0, len=str.length; i<len; ++i){
var cc = str.charCodeAt(i);
if(cc < 128){
//single byte
arr[j++] = cc;
}else{
//UTF-8 multibyte
if(cc < 2048){
arr[j++] = (cc >> 6) | 192;
}else{
arr[j++] = (cc >> 12) | 224;
arr[j++] = ((cc >> 6) & 63) | 128;
}
arr[j++] = (cc & 63) | 128;
}
}
var byteArray = new Uint8Array(arr);
}else{
var byteArray = new Uint8Array(str.length);
for(var i = str.length; i--; )
byteArray[i] = str.charCodeAt(i);
}
return byteArray.buffer;
}
function arrayBufferToString(buffer){
var byteArray = new Uint8Array(buffer);
var str = "", cc = 0, numBytes = 0;
for(var i=0, len = byteArray.length; i<len; ++i){
var v = byteArray[i];
if(numBytes > 0){
//2 bit determining that this is a tailing byte + 6 bit of payload
if((cc&192) === 192){
//processing tailing-bytes
cc = (cc << 6) | (v & 63);
}else{
throw new Error("this is no tailing-byte");
}
}else if(v < 128){
//single-byte
numBytes = 1;
cc = v;
}else if(v < 192){
//these are tailing-bytes
throw new Error("invalid byte, this is a tailing-byte")
}else if(v < 224){
//3 bits of header + 5bits of payload
numBytes = 2;
cc = v & 31;
}else if(v < 240){
//4 bits of header + 4bit of payload
numBytes = 3;
cc = v & 15;
}else{
//UTF-8 theoretically supports up to 8 bytes containing up to 42bit of payload
//but JS can only handle 16bit.
throw new Error("invalid encoding, value out of range")
}
if(--numBytes === 0){
str += String.fromCharCode(cc);
}
}
if(numBytes){
throw new Error("the bytes don't sum up");
}
return str;
}
标题说明了一切。 我有一个 jquery 序列化数据,如下所示:
tarid=value&tarname=value&sel=3
我想把它转换成ArrayBuffer。 之后,我还需要将它重新变回原来的样子。 那我该怎么做呢?
够了吗?
function stringToArrayBuffer(str){
if(/[\u0080-\uffff]/.test(str)){
throw new Error("this needs encoding, like UTF-8");
}
var arr = new Uint8Array(str.length);
for(var i=str.length; i--; )
arr[i] = str.charCodeAt(i);
return arr.buffer;
}
function arrayBufferToString(buffer){
var arr = new Uint8Array(buffer);
var str = String.fromCharCode.apply(String, arr);
if(/[\u0080-\uffff]/.test(str)){
throw new Error("this string seems to contain (still encoded) multibytes");
}
return str;
}
或者您需要真正的 UTF-8 编码
编辑:完整的 UTF-8 支持
Beware/Disclaimer: 此代码未针对 UTF-8 编码器或解码器的某些外国实现进行测试。它可能会产生错误的结果。
在生产中使用之前,请自行测试!
function stringToArrayBuffer(str){
if(/[\u0080-\uffff]/.test(str)){
var arr = new Array(str.length);
for(var i=0, j=0, len=str.length; i<len; ++i){
var cc = str.charCodeAt(i);
if(cc < 128){
//single byte
arr[j++] = cc;
}else{
//UTF-8 multibyte
if(cc < 2048){
arr[j++] = (cc >> 6) | 192;
}else{
arr[j++] = (cc >> 12) | 224;
arr[j++] = ((cc >> 6) & 63) | 128;
}
arr[j++] = (cc & 63) | 128;
}
}
var byteArray = new Uint8Array(arr);
}else{
var byteArray = new Uint8Array(str.length);
for(var i = str.length; i--; )
byteArray[i] = str.charCodeAt(i);
}
return byteArray.buffer;
}
function arrayBufferToString(buffer){
var byteArray = new Uint8Array(buffer);
var str = "", cc = 0, numBytes = 0;
for(var i=0, len = byteArray.length; i<len; ++i){
var v = byteArray[i];
if(numBytes > 0){
//2 bit determining that this is a tailing byte + 6 bit of payload
if((cc&192) === 192){
//processing tailing-bytes
cc = (cc << 6) | (v & 63);
}else{
throw new Error("this is no tailing-byte");
}
}else if(v < 128){
//single-byte
numBytes = 1;
cc = v;
}else if(v < 192){
//these are tailing-bytes
throw new Error("invalid byte, this is a tailing-byte")
}else if(v < 224){
//3 bits of header + 5bits of payload
numBytes = 2;
cc = v & 31;
}else if(v < 240){
//4 bits of header + 4bit of payload
numBytes = 3;
cc = v & 15;
}else{
//UTF-8 theoretically supports up to 8 bytes containing up to 42bit of payload
//but JS can only handle 16bit.
throw new Error("invalid encoding, value out of range")
}
if(--numBytes === 0){
str += String.fromCharCode(cc);
}
}
if(numBytes){
throw new Error("the bytes don't sum up");
}
return str;
}