Skip to content

Instantly share code, notes, and snippets.

@neuthral
Created March 16, 2023 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neuthral/bcf2985169e58bb52fa017a6bfc4c866 to your computer and use it in GitHub Desktop.
Save neuthral/bcf2985169e58bb52fa017a6bfc4c866 to your computer and use it in GitHub Desktop.
js encode image data to ascii and back to Uint8Array
const fs = require("fs");
let imageFile = fs.readFileSync('image.jpg', null).buffer
let imgArray = new Uint8Array(imageFile)
let text = ''
imgArray.forEach(n => {
text += String.fromCharCode(n + 256)
})
console.table(imgArray, text, '\n')
let arr = []
for (let i = 0; i < text.length; i++) {
arr[i] = text.charCodeAt(i) - 256
}
console.table(new Uint8Array(arr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment