We're All Mad Here
We're All Mad Here

Reputation: 1554

PHP get value of char?

I have a single char, $x[0]. Now, I want to get an unsigned char from 0-255. How to do this? Also, if I have 4 chars, $x[0], $x[1], $x[2], $x[3] I want to get an unsigned int from 0-2^32-1 out of those chars.

I hope I make sense. As a C programmer I am not sure those things are possible in PHP.

Upvotes: 1

Views: 99

Answers (2)

Gumbo
Gumbo

Reputation: 655589

You can use ord for a single byte and unpack for one or multiple bytes:

$byte = ord('A');
$dword = (ord('A') << 24) | (ord('B') << 16) | (ord('C') << 8) | ord('D');

$byte = current(unpack('c', 'A'));
$dword = current(unpack('N', 'ABCD'));

Upvotes: 3

Nouphal.M
Nouphal.M

Reputation: 6344

If you are needing the ascii value of a character then you can use ord()

See demo here

Upvotes: 2

Related Questions