如何使用JavaScript在bytearray中转换字符串.输出应该等于下面的C#代码.
UnicodeEncoding encoding = new UnicodeEncoding();
byte[] bytes = encoding.GetBytes(AnyString);
Run Code Online (Sandbox Code Playgroud)
由于UnicodeEncoding默认为UTF-16和Little-Endianness.
编辑:我需要使用上面的C#代码将bytearray生成的客户端与服务器端生成的客户端匹配.
function intFromBytes( x ){
var val = 0;
for (var i = 0; i < x.length; ++i) {
val += x[i];
if (i < x.length-1) {
val = val << 8;
}
}
return val;
}
function getInt64Bytes( x ){
var bytes = [];
var i = 8;
do {
bytes[--i] = x & (255);
x = x>>8;
} while ( i )
return bytes;
}
Run Code Online (Sandbox Code Playgroud)
我试图将javascript编号转换为字节数组,然后返回到数字.但是,上述功能会产生非常大数量的错误输出.
var array = getInt64Bytes(23423423);
var value = intFromBytes(array);
console.log(value); //Prints 23423423 - correct …Run Code Online (Sandbox Code Playgroud) javascript ×2