Skip to content

Instantly share code, notes, and snippets.

@joshxyzhimself
Forked from tomfa/JSON-intArray-converter.js
Created February 26, 2021 23:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshxyzhimself/e9374013022729a4f11f5298f963a9fc to your computer and use it in GitHub Desktop.
Save joshxyzhimself/e9374013022729a4f11f5298f963a9fc 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