Two useful functions from NfWebCrypto for converting Uint8Arrays to Strings, and Strings to Uint8Arrays:
text2ua:function(s) {
var ua = new Uint8Array(s.length);
for (var i = 0; i < s.length; i++) {
ua[i] = s.charCodeAt(i);
}
return ua;
},
ua2text:function(ua) {
var s = '';
for (var i = 0; i < ua.length; i++) {
s += String.fromCharCode(ua[i]);
}
return s;
},
My own function for converting Uint8Arrays to hex:
ua2hex:function(ua) {
var h = '';
for (var i = 0; i < ua.length; i++) {
h += "\\0x" + ua[i].toString(16);
}
return h;
},
ArrayBuffer concatenation I wrote and later found on StackOverflow:
var catArray = new Uint8Array(arrayONE.byteLength+arrayTWO.byteLength);
catArray.set(new Uint8Array(arrayONE),0);
catArray.set(new Uint8Array(arrayTWO), arrayONE.byteLength);
Another useful function to convert a Uint8Array to a Base64-encoded string from StackOverflow:
var ua2b64 = btoa(String.fromCharCode.apply(null, yourUint8Array));
var b642ua = new Uint8Array(atob(yourBase64EncodedString).split("").map(function(c) {
return c.charCodeAt(0); }));
That's all for now. I hope these functions help you as much as they have helped me!
No comments:
Post a Comment