Mikkel
Mikkel

Reputation: 43

Treat array values as hex not ascii

I have a string of hex bytes, like so:

4E44B4096780031713077AB80040052F2F0C1335000000046D372B27230F150E04000000FFFFFF020000FFFFFFFFFF

I split these into bytes by using

$arr1 = str_split($str, 2);

That gives me a nice array of the bytes. However, PHP seems to treat the array elements as ascii, not hex. That makes it impossible for me to make bitmasks etc on them.

How can I declare these bytes as hex?

Upvotes: 0

Views: 384

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

Hexadecimal and even more ASCII are not types of variable and this, in any language.

ASCII is only a character table, not a string.
Hexadecimal is a way to represent a number in base 16, but this representation itself is no longer a number but a character string.

Converting an hexadecimal string to decimal can be done with the function hexdec. Since you have an array of hexadecimal strings, you can use array_map to transform each item.

$result = array_map('hexdec', str_split($str, 2));

Upvotes: 1

Related Questions