Skip to content

Instantly share code, notes, and snippets.

@taniarascia
Created January 1, 2019 00:57
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 taniarascia/c36c4e18703073ae9a2f19e88fabdb14 to your computer and use it in GitHub Desktop.
Save taniarascia/c36c4e18703073ae9a2f19e88fabdb14 to your computer and use it in GitHub Desktop.
16-bit
// echo -en "\x01\x02\x03\x04\x05\x06hello world\x07\x08\x09\x10goodbye world\x11\x12\x13\x14\x15" > data
let fs = require('fs')
let file = process.argv.slice(2)[0]
let buffer = fs.readFileSync(file)
let lines = []
for (let i = 0; i < buffer.length; i += 16) {
let block = buffer.slice(i, i + 16)
let hexArray = []
let chunks = []
let arr = []
for (let value of block) {
let hex = value.toString(16).padStart(2, '0')
hexArray.push(hex)
}
// make chunks of two bytes - [01, 02]
for (let j = 0; j < hexArray.length; j += 2) {
chunks.push(hexArray.slice(j, j + 2))
}
for (let chunk of chunks) {
arr.push((parseInt(chunk[0], 16) << 8) | (parseInt(chunk[1], 16) << 0))
}
let string = arr.join(' ')
lines.push(string)
}
let values = lines.join('\n')
console.log(values)
// Output
// 258 772 1286 26725 27756 28448 30575 29292
// 25607 2057 4199 28527 25698 31077 8311 28530
// 27748 4370 4884 5376
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment