#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Convert Big Endian to Hex


    I am trying to write a PHP script where I can take a big endian packet and convert it over to hex. I understand you can use unpack if anyone can explain the steps I greatly appreciate it. I was told I needed to flip the bits and add one but I do not know if unpack already does that or how to do it. All help greatly appreciated.

    This is taken out of the documentation for the message structure. Messages passed between an application and the interface have the same basic packet structure. This structure consists of a 20 byte IP Header, an 8 byte UDP header, the header and the message. Note that all bytes in multi-byte fields are transmitted in Net Endian format (Big Endian) where the most significant bits are transmitted first. For example, for a 32-bit field, bits 31-24 are transmitted first, 16-23 second, 8-15 third and 0-7 last
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    After reading the manual for less than 2 minutes, I came up with:
    PHP Code:
    $hexArray unpack('H*'$theString); 
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    2
    Rep Power
    0

    Thank you


    Originally Posted by ManiacDan
    After reading the manual for less than 2 minutes, I came up with:
    PHP Code:
    $hexArray unpack('H*'$theString); 
    Does unpacking with H* automatically flip the bits and add one for the twos complement. Thank you
  6. #4
  7. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    H, as specified in the manual, is for big-endian hex. The manual may also have something about the twos-compliment.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  8. #5
  9. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    Does unpacking with H* automatically flip the bits and add one for the twos complement. Thank you
    It does not, it outputs the bits exactly as they are fed to it.

    Two's complement is used for representing positive and negative integers. I'm not certain that's relevant to what you're doing.

    Note that all bytes in multi-byte fields are transmitted in Net Endian format (Big Endian) where the most significant bits are transmitted first. For example, for a 32-bit field, bits 31-24 are transmitted first, 16-23 second, 8-15 third and 0-7 last
    This is a bit confusing.

    The first sentence describe big-endian bit-order, but whether or not it's also implying big-endian byte-order is unclear.

    [[31-24][16-23][8-15][0-7]] describes big-endian byte-order
    [31-24] implies big-endian bit-order
    however
    [16-23], [8-15], [0-7] all imply little-endian bit-order

    Do you have an example of the input and output you're trying to achieve?
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

IMN logo majestic logo threadwatch logo seochat tools logo