Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2004
    Location
    malaysia
    Posts
    44

    converts a u_long from host to TCP/IP network byte order (which is big-endian)

    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...

  2. #2
    Join Date
    Mar 2004
    Posts
    80
    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.
    Last edited by Ziad; 04-22-2004 at 01:43 AM.

  3. #3
    Join Date
    Mar 2004
    Posts
    80

  4. #4
    Join Date
    Apr 2004
    Location
    malaysia
    Posts
    44
    Thanks you much for your nice help..

  5. #5
    Join Date
    Apr 2004
    Location
    malaysia
    Posts
    44
    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...

  6. #6
    Join Date
    Mar 2004
    Posts
    80

  7. #7
    Join Date
    Apr 2004
    Location
    malaysia
    Posts
    44
    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>";

  8. #8
    Join Date
    Apr 2004
    Location
    malaysia
    Posts
    44
    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.

  9. #9
    Join Date
    Apr 2004
    Location
    malaysia
    Posts
    44
    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

  10. #10
    Join Date
    Feb 2003
    Posts
    33
    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
    Check out my new blog at http://www.blog.jydesign.biz

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •