Skip to content

Instantly share code, notes, and snippets.

@og24715
Last active June 13, 2018 01:48
Show Gist options
  • Save og24715/0cc1e452cf971f82cd3d87db4dcf5003 to your computer and use it in GitHub Desktop.
Save og24715/0cc1e452cf971f82cd3d87db4dcf5003 to your computer and use it in GitHub Desktop.
バイナリ職人

hex string -> buffer

const hexString = '00AA55FF';
const buffer = Buffer.from(hexString, 'hex'); // <Buffer 00 aa 55 ff>

const firstByte = buffer.slice(1, 3) // <Buffer aa 55>

buffer -> hex string

const buffer = Buffer.from([0x00, 0xaa, 0x55, 0xff]); // <Buffer 00 aa 55 ff>
const hexString = buffer.toString('hex'); // '00AA55FF'

little endian

const buffer = Buffer.from([0x00, 0xaa, 0x55, 0xff]); // <Buffer 00 aa 55 ff>
buffer.readUIntLE(0, 2); // <Buffer aa 00>

String -> Ascii code

const str = 'abcABC';
Buffer.from(str).toString('hex'); // -> '616263414243'
str.split('').map(c => c.charCodeAt(0).toString(16)).join('') // -> '616263414243'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment