All pastes #42179 Raw Edit

Someone

public text v1 · immutable
#42179 ·published 2006-02-20 03:42 UTC
rendered paste body
I am working on a networking script in PHP using PHP's socket functions.  One thing I noticed was a lack of a way to convert integers to WORDs and DWORDs.  Google does not help me in any way, but I did find something that is kind of helpful.  Someone posted this bit of code on a forum.
[code]   /* Create a little-endian 16-bit integer (WORD) */
  function make_int16($w) {
  $A = (((int)$w & 0x00FF) >> 0);
  $B = (((int)$w & 0xFF00) >> 8);
    return chr($A) . chr($B);
  }
  /* Create a little-endian 32-bit integer (DWORD) */
  function make_int32($d) {
    $A = (((int)$d & 0x000000FF) >> 0);
    $B = (((int)$d & 0x0000FF00) >> 8);
    $C = (((int)$d & 0x00FF0000) >> 16);
    $D = (((int)$d & 0xFF000000) >> 24);
    return chr($A) . chr($B) . chr($C) . chr($D);
  }[/code]

I don't know if this bit of code works because my outgoing packet structure needs to be fixed, so either way it won't work and I need to write the code to receive packets.

So, I need to know if this code is correct and that is the right way to convert hex values to WORDs and DWORDs and I also want to know if someone is willing to explain how it works for me (bitwise math isn't a very strong suit of mine).