Skip to content

Instantly share code, notes, and snippets.

@jbflow
Last active March 7, 2024 20:34
Show Gist options
  • Save jbflow/bd274354f0ce4e4b5f088eddbf0189f3 to your computer and use it in GitHub Desktop.
Save jbflow/bd274354f0ce4e4b5f088eddbf0189f3 to your computer and use it in GitHub Desktop.
functions for converting data to send over syses
const convertBytes = (byteArray) => {
// converts arrays of 2 or 3 bytes into there integer values
if (byteArray.length === 2) {
return byteArray[1] * 128 + byteArray[0];
} else if (byteArray.length === 3) {
return byteArray[2] * 16384 + byteArray[1] * 128 + byteArray[0];
}
}
const convertInt = (integer) => {
// splits integers up to 2097152 into three bytes
const bytes = [
Math.floor(integer % 128),
Math.floor((integer / 128) % 128),
Math.floor((integer / Math.pow(128, 2)) % 128)
];
return bytes
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment