Logo

Programming-Idioms

History of Idiom 176 > diff from v6 to v7

Edit summary for version 7 by 1.7.4:
[JS] Fixed so it worked :3

Version 6

2019-01-24, 13:35:04

Version 7

2019-01-24, 13:45:08

Idiom #176 Hex string to byte array

From hex string s of 2n digits, build the equivalent array a of n bytes.
Each pair of hexadecimal characters (16 possible values per digit) is decoded into one byte (256 possible values).

Idiom #176 Hex string to byte array

From hex string s of 2n digits, build the equivalent array a of n bytes.
Each pair of hexadecimal characters (16 possible values per digit) is decoded into one byte (256 possible values).

Extra Keywords
hexa
Extra Keywords
hexa
Code
const base = 16
let a = s
  .replace(/../, '$&_') // hi perl
  .slice (0, -1) // take off the last one
  .split ('_')
  .map (
    ([x, y]) => parseInt (x, 16) * 16 + parseInt (y, 16)
  )
Code
const base = 16
let a = s
  .replace(/../g, '$&_')
  .slice (0, -1)
  .split ('_')
  .map (
    (x) => parseInt (x, 16)
  )
Comments bubble
• seperate every pair of digits with _
• throw out the last '_'
• turn 'this_thing' to ['this','thing']
• read the pairs as base 16