View Full Version : converts a u_long from host to TCP/IP network byte order (which is big-endian)
stephenvs 04-22-2004, 01:08 AM E.g (vc++)
lNow=4221114;
ulNetVal = htonl (lNow);
ulNetVal give result 3127394304
In Vc++ htonl function can converts a u_long from host to TCP/IP network byte order (which is big-endian)
In PHP, Is there is any way we can do a convertioin like above vc++ example
My wrong understanding, If i use pack function. i expect the answer like above, But it result like this
<?php echo pack("N", "4221007"); ?> This is the result @hO
What should i do in order to get the result like above,
i.e converts a u_long from host to TCP/IP network byte order (which is big-endian).
Thanks in advance...:confused:
The result that you get is the correct one.
@ has value 64
h has value 104
O has value 79
64*256^2 + 104*256 +79 = 4221007
You just have to interpret the string as a u_long.
HTH
*** EDIT ***
After looking at the function specs on php.net I believe you have to use V and not N. So that your number that you give to pack() will be interpreted in the reverse order.
*** EDIT #2 ***
I've tried 4221114 with the V parameter and it gives back
ºh@_ (the last _ is just a null character)
which is:
186*256^3 + 104*256^2 + 64*256 = 3127394304
just like you want it to be.
Sorry for the double post but can't edit after 15 minutes
stephenvs 04-22-2004, 03:40 AM Thanks you much for your nice help..
stephenvs 04-22-2004, 05:39 AM Sorry again
186*256^3 + 104*256^2 + 64*256 = 3127394304
i.e 186*256^3=3120562176 (By Calculator) which is correct
But when i do it PHP 186*256^3=47619, ^ operator(Dont know how to implment it)
$BigEndian=pack ('V', 4221114);
for($i=1;$i<(strlen($BigEndian)+1);$i++)
{echo $AscIn+=ord(substr($BigEndian,-$i,1))*256^3;
echo "<br>";}
kindly aske for the help...
$val = unpack('Nbig', pack('V', 4221114));
echo $val['big'];
Returns -1167572992 which is the number you want (but interpreted as signed). I think you can just use that value in the network function.
stephenvs 04-23-2004, 12:01 AM Actually i am trying to convert some source from vc++ to PHP, Follow some server protocol and client sent a data to server.
************************************
The following is the VC++Source Code
************************************
This are the value passed to through the variables underneath mathemetical function
end_byte=9
end_bit=0
value=3868147712
************************************
byte_pos = end_byte;
width_mask = (BYTE) (((1 << (7 - end_bit)) - 1) << 1 | 1);
byte_value = (BYTE) (value & width_mask);
byte_value <<= end_bit;
value >>= (7 - end_bit + 1);
or_mask = (BYTE) (width_mask << end_bit);
and_mask = (BYTE) (~or_mask);
p_buff[byte_pos] &= and_mask;
p_buff[byte_pos] |= byte_value;
byte_pos--;
************************************
If u can explain what is going on here, it would be much easier rather than just apply this, more over vc++ return different value and php return different value, and also i cant find the relevant functions in php, my code is like this
$byte_pos = $end_byte;
echo "BytPost=$byte_pos";echo "<br>";
$width_mask = decbin(((1 << (7 - $end_bit)) - 1) << 1 | 1);
echo "2width_mask=$width_mask"."<br>";
echo "value=$value";echo "<br>";
$byte_value = (decbin($value) & $width_mask);
echo "end_bit=$end_bit"."<br>";
$byte_value <<= $end_bit;
echo "Bytevalue=$byte_value"."<br>";
$value >>= (7 - $end_bit + 1);
echo "value=$value";echo "<br>";
$or_mask = ($width_mask << $end_bit);
$and_mask = (~$or_mask);
$p_buff[$byte_pos] &= $and_mask;
$bits="";
for($i=0;$i<($len-strlen($byte_value));$i++){$bits.="0";}
$byte_value=$bits.$byte_value;
echo "2byte_value=$byte_value"."<br>";
$p_buff[$byte_pos] |= $byte_value;echo "2p_buff[$start_byte]=$p_buff[$start_byte]"."<br>";
$byte_pos--;
echo "ByteDecre--=".$byte_pos."<br>";
stephenvs 04-23-2004, 12:42 AM Vc++ retruns
(htons=16-bit number in host byte order. )
(htonl=32-bit number in host byte order.)
nmax=16
ulNetVal = (unsigned long) htons ((unsigned short) (nMax + 2));
ulNetVal returns 4608,
PHP
For (htonl=32-bit number in host byte order.)
$val = unpack('Nbig', pack('V', 4221114));
echo $val['big'];
which is correct 32bit
for 16Bit, if u would help me much easier, thanks for your helps.
stephenvs 04-27-2004, 12:23 AM Hi there,
can u pls help me, how to make the number for eg 18 into 2bytes i.e 16 bits
$ulNetVal =pack('N', 18)."<br>";
Thanks in advance
bitziz 04-27-2004, 12:55 AM You're probably not going to like what I'm going to say but still...
Always read the manual first...
you said you didn't know how to implement the ^ operator... i'm sure you know x^y means x rised to the power of y. Please look at this page:
http://www.php.net/manual/en/ref.math.php
As a matter of fact, looking through that manual will probably help you a lot. I would focus on the socket library among others. "ALWAYS look and think before you ask, not only do you learn more you also avoid looking like an ***" That's what my dad use to say to me ;)
|