Skip to content

Core\Encoding::fromHex

Reads hexadecimal text back into the bytes it was made from.

Hexadecimal writes one byte as two digits, from 00 to ff. It is the form you see wherever a person has to read bytes: a checksum beside a download, a colour in a stylesheet, a key in a configuration file. Core\Encoding::fromHex is the other half of Core\Encoding::toHex. It reads the digits 0 to 9 and the letters a to f in either case, and nothing else. No 0x in front, and no space, colon or newline between the pairs. A text with an odd number of digits is an error, because one byte takes two digits. The error says which of the two things is wrong.

Good to know: Core\Encoding::toHex always writes lower case, and both cases read back to the same bytes. Compare the bytes rather than the text, and the case stops mattering.

Parameters

  • $sstringThe hexadecimal text.

Return value

Returns bytes. The decoded bytes, one byte for every two digits of $s. The empty string gives empty bytes.

Errors

  • RuntimeError$s has an odd number of digits, or a character that is not 0-9, a-f or A-F. A space, a colon and a 0x prefix all throw this error.

Examples

Bytes a person can read

<?nvs
// Shows the round trip: bytes go out as hexadecimal text, and the same bytes
// come back.
bytes $raw = Core\Encoding::fromHex('deadbeef');
// `Core\Encoding::toHex` writes the text, always in lower case. This prints
// deadbeef.
echo Core\Encoding::toHex($raw), "\n";
// Upper case reads back to the same bytes, so this prints deadbeef too.
echo Core\Encoding::toHex(Core\Encoding::fromHex('DEADBEEF')), "\n";
// Two digits are one byte. This prints 4.
echo Core\Bytes::length($raw), "\n";
// The empty string gives an empty buffer. This prints 0.
echo Core\Bytes::length(Core\Encoding::fromHex('')), "\n";
Output
deadbeef
deadbeef
4
0

The digits and nothing else

<?nvs
// Shows what belongs in the text and what has to be taken out first.
// A colour from a stylesheet is three bytes: red, green and blue. The `#` is
// not a digit, so it is cut off first. This prints 47 111 78.
bytes $colour = Core\Encoding::fromHex(Core\Str::slice('#2f6f4e', 1, null));
echo Core\Bytes::at($colour, 0), ' ', Core\Bytes::at($colour, 1), ' ', Core\Bytes::at($colour, 2), "\n";
// Nothing may stand between the pairs either. A network address is written
// with colons, so take them out with `Core\Str::replace`. This prints 6.
string $address = '00:1b:44:11:3a:b7';
echo Core\Bytes::length(Core\Encoding::fromHex(Core\Str::replace($address, ':', ''))), "\n";
// A `0x` in front belongs to a number in source code, not to this text.
try {
Core\Encoding::fromHex('0xff');
} catch (RuntimeError $prefixed) {
echo 'refused: ', $prefixed->message, "\n";
}
// An odd number of digits is an error, because one byte takes two digits.
try {
Core\Encoding::fromHex('abc');
} catch (RuntimeError $odd) {
echo 'refused: ', $odd->message, "\n";
}
Output
47 111 78
6
refused: Core\Encoding::fromHex(): "0xff" is not hexadecimal — only the digits `0`-`9`, `a`-`f` and `A`-`F` spell one, with nothing between the pairs
refused: Core\Encoding::fromHex(): "abc" is not hexadecimal — it has an odd number of digits, and an octet takes two

A checksum beside a download

<?nvs
// Shows the everyday use: a file is published with its checksum in
// hexadecimal, and your program checks the file against it.
// What you downloaded, and the checksum the site printed beside it.
bytes $download = Core\Encoding::encodeText('hello', Core\Charset::Utf8);
string $published = '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824';
// The checksum of what you have. This prints the same 64 digits.
bytes $mine = Core\Hash::of($download, Core\Hash::Digest::Sha256);
echo Core\Encoding::toHex($mine), "\n";
// Compare the bytes, not the text: the site may print the digits in upper
// case. `Core\Bytes::compare` gives 0 here, so both lines print 0.
echo Core\Bytes::compare($mine, Core\Encoding::fromHex($published)), "\n";
echo Core\Bytes::compare($mine, Core\Encoding::fromHex(Core\Str::upper($published))), "\n";
// One digit changed is a different file. This prints different.
string $changed = '3cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824';
echo (Core\Bytes::compare($mine, Core\Encoding::fromHex($changed)) == 0 ? 'same' : 'different'), "\n";
Output
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
0
0
different