Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2013
    Location
    Texas
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Convert Decimal to Two Byte Binary Representation

    I'm currently coding to another company's specs and need to convert a decimal (byte size of payload being posted to their servers) to a two byte binary representation.

    I'm fairly new to binary numbers other than the basics you learn in school. The specs of the company I'm connecting to include the following example:

    ...place 502 binary (0x01 0xF6) two-byte header...
    I've been working with PHP in order to produce the same results they use in their example. However, while I can use dexhex to produce 1f6 as a string, it's not two bytes and it's not represented in the format I need for my communcation.

    PHP Code:
    $var dechex502 ); // produces string 1f6 (3 bytes) (no good)
    $var decbin502 ); //produces string 111110110 (9 bytes) (no good) 
    I would greatly appreciate some help on this subject or at least some pointers to get me going in the correct direction. Thank you very much for your time.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Pack it up instead to force it into binary:
    PHP Code:
    $var 502;
    $packed pack('n'$var);

    printf("Packed hex is: %s"bin2hex($packed)); 
    I can't inspect it from where I am, but the 'n' should be an unsigned short in big endian order, so that should be 2 bytes in size.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New to the CF scene
    Join Date
    Mar 2013
    Location
    Texas
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you very much Fou-Lu! This is exactly what I'm looking. Should the pack type not match up properly with the remote system, I'm sure I can get the correction I need from the other company to make sure it works from here. Very grateful for your help!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Yep, that should match what they are expecting. Short is used to classify the length of the data as being 16bits from the integer which is 32. So it will simply truncate off the highest order bytes and block it into the short size and pack the binary. Once its in binary format, than there is no difference between 502 and 0x1f6 only how its interpreted after the fact (it could be interpreted as SOH + � if it wanted to be).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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