Skip to content

Instantly share code, notes, and snippets.

@vongvisalsambath
Forked from zuk/byte_operations.js
Created December 5, 2018 10:22
Show Gist options
  • Save vongvisalsambath/81fc82167a21f1b1c7e1b29895d8a4f8 to your computer and use it in GitHub Desktop.
Save vongvisalsambath/81fc82167a21f1b1c7e1b29895d8a4f8 to your computer and use it in GitHub Desktop.
How to convert between integers and bytes in JavaScript
// Here's how to do basic integer<->byte (string/char) operations in JavaScript.
// Took me a good while to dig this up.
function intToChar(integer) {
return String.fromCharCode(integer)
}
function charToInt(char) {
return char.charCodeAt(0)
}
// Example:
intToChar(90) // --> "Z"
charToInt("Z") // --> 90
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment