This language bar is your friend. Select your favorite languages!

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).

for i := 0 to length(s) div 2 - 1 do
  a[i] := StrToInt('$'+Copy(s,2*(i)+1,2));
import std.algorithms;
import std.array;
import std.conv;
import std.range;
ubyte[] a = s.chunks(2)
             .map!(digits =>!ubyte)
import std.conv;
import std.string;
ubyte[] a = cast(ubyte[]) hexString!s.representation;
import "encoding/hex"
a, err := hex.DecodeString(s)
if err != nil {
const base = 16
let a = s
  .replace(/../g, '$&_')
  .slice (0, -1)
  .split ('_')
  .map (
    (x) => parseInt (x, 16)
a = bytearray.fromhex(s)

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

Related idioms