Atom

binary.unhex()

Decodes hexadecimal byte pairs.

Signature

binary.unhex(data)

Parameters

Text or packed span containing ASCII digits, with upper or lower case A–F.

Returns

Packed u8 bytes.

Behavior

Accepts empty input.

Errors

Odd length, invalid digit or unsupported input.

Example

Atom
use binary
assert(binary.text(binary.unhex('41')) == 'A')

Decode two hex digits

Give unhex hexadecimal text when you need the underlying bytes.

Atom
use binary
print(binary.text(binary.unhex('41')))
Output
A

The hex pair 41 represents the byte for A.

Decode a packed ASCII input

Use a byte buffer when the hexadecimal digits came from binary input.

Atom
use binary
text_bytes = binary.utf8('41')
print(binary.text(binary.unhex(text_bytes)))
Output
A

unhex reads the ASCII digits from the buffer and returns the same decoded byte.