Last active
March 7, 2024 20:34
-
-
Save jbflow/bd274354f0ce4e4b5f088eddbf0189f3 to your computer and use it in GitHub Desktop.
functions for converting data to send over syses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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