Core\Encoding::toHex
Core\Encoding::toHex(bytes $b): stringCore\Encoding::toHex( bytes $b, ): string
Writes bytes as hexadecimal text, so that a person can read them.
Hexadecimal writes one byte as two digits, from 00 to ff. Every byte has a spelling, so this member never fails. The text is always twice as long as the buffer, and the empty buffer gives the empty string. Core\Encoding::toHex writes the digits 0 to 9 and the letters a to f in lower case. It writes nothing else: no 0x in front, and no space or colon between the pairs. Add those yourself when the form you are writing for wants them.
This is the form you meet wherever bytes have to be read by a person: a checksum beside a download, a colour in a stylesheet, a key in a configuration file, a few bytes in a log line.
Good to know: Core\Encoding::fromHex reads the text back into bytes, and it accepts upper case as well as lower case.
Parameters
$bbytesThe bytes to encode.
Return value
Returns string. The hex text, twice as long as $b. Empty bytes give the empty string.
Examples
Two digits for every byte
<?nvs// Shows what the text looks like: two digits for every byte, always in lower// case.
bytes $raw = Core\Encoding::encodeText('hello', Core\Charset::Utf8);
// One byte is two digits. This prints 68656c6c6f.echo Core\Encoding::toHex($raw), "\n";
// So the text is twice as long as the buffer. This prints 5 10.echo Core\Bytes::length($raw), ' ', Core\Str::length(Core\Encoding::toHex($raw)), "\n";
// The letters `a` to `f` are written in lower case, whatever case the bytes// were read from. This prints ff00ab.echo Core\Encoding::toHex(Core\Encoding::fromHex('FF00AB')), "\n";
// The empty buffer gives the empty string, so this line prints nothing.echo Core\Encoding::toHex(Core\Encoding::encodeText('', Core\Charset::Utf8)), "\n";68656c6c6f5 10ff00abWhat you add yourself
<?nvs// Shows what the text does not carry, and how to add what a form wants.
// A colour is three bytes: red, green and blue. A stylesheet wants a `#` in// front, and this member does not write one. This prints #2f6f4e.bytes $colour = Core\Encoding::fromHex('2f6f4e');echo '#', Core\Encoding::toHex($colour), "\n";
// There is nothing between the pairs either. A network address is written with// a colon after every byte, so take the digits two at a time.string $hex = Core\Encoding::toHex(Core\Encoding::fromHex('001b44113ab7'));string $address = '';int $i = 0;while ($i < Core\Str::length($hex)) { if ($i > 0) { $address = $address . ':'; } $address = $address . Core\Str::slice($hex, $i, 2); $i = $i + 2;}// This prints 00:1b:44:11:3a:b7.echo $address, "\n";
// Upper case is one more call. This prints 001B44113AB7.echo Core\Str::upper($hex), "\n";#2f6f4e00:1b:44:11:3a:b7001B44113AB7Bytes in a log line
<?nvs// Shows the everyday use: a program is given a file it does not understand,// and puts the first bytes in its log so a person can see them.
// A PNG image starts with these eight bytes.bytes $pngHeader = Core\Encoding::fromHex('89504e470d0a1a0a');
// What somebody uploaded. This one is a GIF image.bytes $upload = Core\Encoding::encodeText('GIF89a and then the pixels', Core\Charset::Ascii);
// This prints the upload is not a PNG.if (!Core\Bytes::startsWith($upload, $pngHeader)) { echo "the upload is not a PNG\n";}
// The bytes are not text, and a log line has to be readable. The first eight// bytes are sixteen digits. This prints starts with 4749463839612061.echo 'starts with ', Core\Encoding::toHex(Core\Bytes::slice($upload, 0, 8)), "\n";
// The same eight bytes printed as they are. Most of them happen to be letters// here. A file of real pixels prints characters nobody can read.echo 'as characters ', Core\Encoding::decodeText(Core\Bytes::slice($upload, 0, 8), Core\Charset::Ascii), "\n";the upload is not a PNGstarts with 4749463839612061as characters GIF89a a