PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
kipper11
Forum Newbie
Posts: 2 Joined: Wed Mar 11, 2009 9:23 am
Post
by kipper11 » Wed Mar 11, 2009 9:31 am
Hello everyone,
I want to convert a 32bit Integer (or DWORD) into a chr array with a length of 4 chrs. I've tried using bitwise:
Code: Select all
function dword2chrs($num)
{
$result = "";
for ($x = 3; $x > -1; $x--)
$result .= chr(($num >> (8 * $x)) & 255);
return $result;
}
...but it only seems to work for certain number ranges, so I'm assuming I've messed up somewhere. Can anyone offer me some assistance here?
Regard,
Kip.
kipper11
Forum Newbie
Posts: 2 Joined: Wed Mar 11, 2009 9:23 am
Post
by kipper11 » Wed Mar 11, 2009 10:54 am
Nevermind, I've figured it out for myself.
Code: Select all
function dword2chrs($num)
{
$result = "";
$result .= chr($num & 255);
$result .= chr(($num >> 8) & 255);
$result .= chr(($num >> 16) & 255);
$result .= chr(($num >> 24) & 255);
return $result;
}