Created
January 1, 2019 00:57
-
-
Save taniarascia/c36c4e18703073ae9a2f19e88fabdb14 to your computer and use it in GitHub Desktop.
16-bit
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
// 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