//Uint8Trick.js
//30 Aug 2016
var i;
console.log("Create an object with an Uint8Array");
var A={B:512,C:new Uint8Array(16)};
for(i=0;i<16;i++)A.C[i]=i;
console.log("A= ",A);
console.log("Stringify the object");
var D=JSON.stringify(A);
console.log("D= ",D);
console.log("Parse it back returns undefined");
var F=JSON.parse(D);
console.log("F= ",F); //F is undefined
//notice I skipped E to avoid conflict with E.toUint8Array
//the trick
console.log("apply btoa trick");
A.C=btoa(A.C);
console.log("A= ",A);
console.log("stringify");
var D=JSON.stringify(A);
console.log("D= ",D);
console.log("parse");
var F=JSON.parse(D);
console.log("F= ",F);
//the trick in reverse
console.log("reverse the trick");
F.C=E.toUint8Array(atob(F.C));
console.log("Frev= ",F);
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Try this exercise:
and the output
1 Attachment