Skip to content

Instantly share code, notes, and snippets.

@justintien
Forked from tomfa/JSON-intArray-converter.js
Created February 7, 2021 09:15
Show Gist options
  • Save justintien/20b0248813aa7d9d9543be86efa41704 to your computer and use it in GitHub Desktop.
Save justintien/20b0248813aa7d9d9543be86efa41704 to your computer and use it in GitHub Desktop.
JSON to 8-bit-integer parsing (and visa versa)
// JSON to Uint8Array parsing and visa versa
// (Intended Bluetooth communication on Cordova)
var JsonToArray = function(json)
{
var str = JSON.stringify(json, null, 0);
var ret = new Uint8Array(str.length);
for (var i = 0; i < str.length; i++) {
ret[i] = str.charCodeAt(i);
}
return ret
};
var binArrayToJson = function(binArray)
{
var str = "";
for (var i = 0; i < binArray.length; i++) {
str += String.fromCharCode(parseInt(binArray[i]));
}
return JSON.parse(str)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment