[英]From Java byte[] -> Base64 -> Javascript ArrayBuffer -> Base64 -> Byte[]
I'm having trouble sending data from Java to Javascript and the reverse of that. 我在将数据从Java发送到Javascript时遇到了麻烦,反之亦然。
Ive tried this so far: 到目前为止我试过这个:
//a function I found online I use it to convert the decoded version of the java base64 byte[] to an ArrayBuffer
function str2ab(str) {
var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char
var bufView = new Uint16Array(buf);
for (var i=0, strLen=str.length; i<strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
return buf;
}
function ab2str(buf) {
return String.fromCharCode.apply(null, new Uint16Array(buf));//retuns a different value than what I put into the buffer.
}
I really don't know how to go about this anymore any ideas? 我真的不知道怎么回事这个想法?
using import com.sun.org.apache.xml.internal.security.utils.Base64;
使用import com.sun.org.apache.xml.internal.security.utils.Base64;
byte[] b = new byte[] { 12, 3, 4, 5, 12 , 34, 100 };
String encoded = Base64.encode(b);
produces: 生产:
"DAMEBQwiZA=="
var stringToByteArray = function(str) {
var bytes = [];
for (var i = 0; i < str.length; ++i) {
bytes.push(str.charCodeAt(i));
}
return bytes;
};
var decoded = stringToByteArray(atob("DAMEBQwiZA=="));
produces: 生产:
[ 12, 3, 4, 5, 12 , 34, 100 ]
Note: If you are doing this in NodeJS then take a look at the atob package. 注意:如果您在NodeJS中执行此操作,请查看atob包。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.