Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
}
@DreamAndDead

This comment has been minimized.

Show comment
Hide comment
@DreamAndDead

DreamAndDead May 12, 2016

thanks man, it helps a lot

DreamAndDead commented May 12, 2016

thanks man, it helps a lot

@DreamAndDead

This comment has been minimized.

Show comment
Hide comment
@jghorton14

This comment has been minimized.

Show comment
Hide comment
@jghorton14

jghorton14 Aug 2, 2018

This worked for me: JSON.parse(binArrayToJson(binArray))

jghorton14 commented Aug 2, 2018

This worked for me: JSON.parse(binArrayToJson(binArray))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment