Skip to content

Instantly share code, notes, and snippets.

@tomfa
Created May 10, 2015 11:22
Show Gist options
  • Save tomfa/706d10fed78c497731ac to your computer and use it in GitHub Desktop.
Save tomfa/706d10fed78c497731ac 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)
}
@tomfa
Copy link
Author

tomfa commented Jun 15, 2025

It sounds like you want to reduce network traffic between the server and client. This is well known and optimized problem, and there's various solutions to go for depending on your priorities / most pressing motivation.

  1. are you trying to learn about computer science?
  2. are you trying to minimize costs?
    • do you consider your time spent on the optimization a cost?
  3. do you want to minimize experienced latency?
  4. are you trying to minimize network traffic?

I would not consider doing what you're thinking of in 99.9% of the cases of 2, 3 or 4.

Give me some more info about your situation, and maybe I can give better advice. You could also consider installing Cursor and asking AI - which has better answers and response times than most internet randos like myself

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