1

what is the difference between these two lines of code?

var binStr = Buffer.from(hex, 'hex').toString('binary'); and var asc = Buffer.from(hex, 'hex').toString('ascii');

hex = '4d434750040af215000a00000000000a000000000000000000000029'

for more detail check this link jdoodle.com/a/w51

but when I am converting it back into hex using this https://www.rapidtables.com/convert/number/ascii-to-hex.html

then binStr gives correct output and asc gives wrong.

Can anybody tell me why?

Shubham Batra
  • 2,357
  • 5
  • 29
  • 48
  • 1
    binary is 8 bits, ascii is 7 bits – Jaromanda X May 29 '18 at 06:20
  • then why https://www.rapidtables.com/convert/number/ascii-to-hex.html this site converting ascii wrong and binary right – Shubham Batra May 29 '18 at 06:22
  • 2
    because binary is 8 bits and ascii is 7 bits – Jaromanda X May 29 '18 at 06:23
  • https://stackoverflow.com/questions/14690159/is-ascii-code-7-bit-or-8-bit – gotnull May 29 '18 at 06:25
  • p.s. your ascii-to-hex site is nothing to do with node.js and is irrelevant (note, it handles 8bit hex just fine by the way, though doesn't like anything too complex in hex) – Jaromanda X May 29 '18 at 06:27
  • 1
    @fuzz's link is good reading ... but for nodejs, [read the fine manual](https://nodejs.org/api/buffer.html#buffer_buffers_and_character_encodings) which states that *ascii -For 7-bit ASCII data only. This encoding is fast and **will strip the high bit** if set.* - therefore you'd want *binary* - though that is an alias for `latin1` - however, `latin1` is suitable for binary data – Jaromanda X May 29 '18 at 06:33

0 Answers0