• Welcome to Valhalla Legends Archive.
 

[PHP] Make/Get Int16/Int32 (DWORD/WORD)

Started by Joe[x86], December 14, 2005, 09:27 PM

Previous topic - Next topic

Joe[x86]

Can anyone post these functions? I can't get it right for the life of me. =(
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Joe[x86]

UGH! Leave it to me to forget to call chr().

These should actually work.

  /* 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);
  }
  /* Parse a little-endian 16-bit integer (WORD) */
  function get_int16($data) {
  $A = (int)(substr($data, 0) & 0x000000FF);
  $B = (int)(substr($data, 1) & 0x0000FF00);
  return $A | $B;
  }
  /* Parse a little-endian 32-bit integer (DWORD) */
  function get_int32() {
  $A = ((int)substr($data, 0) & 0x000000FF);
  $B = ((int)substr($data, 1) & 0x0000FF00);
  $B = ((int)substr($data, 2) & 0x00FF0000);
  $B = ((int)substr($data, 3) & 0xFF000000);
  return $A | $B | $C |$D;
  }
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Joe[x86]

MakeDWORD works correctly but GetDWORD doesn't. Anyone want to take a stab at it?
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.


Warrior

Wow Cloaked I forgot you wrote that, very nice.
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

effect

Quote from: Warrior on December 17, 2005, 03:00 PM
Wow Cloaked I forgot you wrote that, very nice.

rofl "Warrior of clan x86" , can we say the new wannabe?

anyways just had to point out that every post you post here is irrelevant, that nothing u post actually is helpful and your a reputation whore.

:o
Quote from: Mangix on March 22, 2005, 03:03 AM
i am an expert Stealthbot VBScript. Recognize Bitch.

Joe[x86]

#6
Yeah, where the hell is my report to moderator button?

EDIT -
I finally got it working, so I might as well post it! =)
<?
  /*
   * Create and parse integral data structures
  */
  /* 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);
  }
  /* Parse a little-endian 16-bit integer (WORD) */
  function get_int16($data) {
    $a = strrev(str_pad(dechex(ord(substr($data, 0, 1))), 2, '0', STR_PAD_LEFT));
    $b = strrev(str_pad(dechex(ord(substr($data, 1, 1))), 2, '0', STR_PAD_LEFT));
    return hexdec(strrev($a . $b));
  }
  /* Parse a little-endian 32-bit integer (DWORD) */
  function get_int32($data) {
    $a = strrev(str_pad(dechex(ord(substr($data, 0, 1))), 2, '0', STR_PAD_LEFT));
    $b = strrev(str_pad(dechex(ord(substr($data, 1, 1))), 2, '0', STR_PAD_LEFT));
    $c = strrev(str_pad(dechex(ord(substr($data, 2, 1))), 2, '0', STR_PAD_LEFT));
    $d = strrev(str_pad(dechex(ord(substr($data, 3, 1))), 2, '0', STR_PAD_LEFT));
    return hexdec(strrev($a . $b . $c . $d));
  }
?>
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Warrior

Not bad you're getting pretty good at PHP :)
Quote from: effect on March 09, 2006, 11:52 PM
Islam is a steaming pile of fucking dog shit. Everything about it is flawed, anybody who believes in it is a terrorist, if you disagree with me, then im sorry your wrong.

Quote from: Rule on May 07, 2006, 01:30 PM
Why don't you stop being American and start acting like a decent human?

shadypalm88

Quote from: Joe on December 22, 2005, 01:50 PM
<?
  /* Parse a little-endian 32-bit integer (DWORD) */
  function get_int32($data) {
    $a = strrev(str_pad(dechex(ord(substr($data, 0, 1))), 2, '0', STR_PAD_LEFT));
    $b = strrev(str_pad(dechex(ord(substr($data, 1, 1))), 2, '0', STR_PAD_LEFT));
    $c = strrev(str_pad(dechex(ord(substr($data, 2, 1))), 2, '0', STR_PAD_LEFT));
    $d = strrev(str_pad(dechex(ord(substr($data, 3, 1))), 2, '0', STR_PAD_LEFT));
    return hexdec(strrev($a . $b . $c . $d));
  }
?>

Oh.  My.  God.

rabbit


Quote from: Joe on December 22, 2005, 01:50 PM
<?
  /* Parse a little-endian 32-bit integer (DWORD) */
  function get_int32($data) {
    $a = strrev(str_pad(dechex(ord(substr($data, 0, 1))), 2, '0', STR_PAD_LEFT));
    $b = strrev(str_pad(dechex(ord(substr($data, 1, 1))), 2, '0', STR_PAD_LEFT));
    $c = strrev(str_pad(dechex(ord(substr($data, 2, 1))), 2, '0', STR_PAD_LEFT));
    $d = strrev(str_pad(dechex(ord(substr($data, 3, 1))), 2, '0', STR_PAD_LEFT));
    return hexdec(strrev($a . $b . $c . $d));
  }
?>


Quote from: shadypalm88 on December 22, 2005, 04:16 PMOh.  My.  God.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Joe[x86]

Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

Douglas

Quote from: rabbit on December 23, 2005, 09:35 AM

Quote from: Joe on December 22, 2005, 01:50 PM
<?
  /* Parse a little-endian 32-bit integer (DWORD) */
  function get_int32($data) {
    $a = strrev(str_pad(dechex(ord(substr($data, 0, 1))), 2, '0', STR_PAD_LEFT));
    $b = strrev(str_pad(dechex(ord(substr($data, 1, 1))), 2, '0', STR_PAD_LEFT));
    $c = strrev(str_pad(dechex(ord(substr($data, 2, 1))), 2, '0', STR_PAD_LEFT));
    $d = strrev(str_pad(dechex(ord(substr($data, 3, 1))), 2, '0', STR_PAD_LEFT));
    return hexdec(strrev($a . $b . $c . $d));
  }
?>


is that written yourself? http://www.php.net/manual/en/ref.strings.php

- Stolen from rabbit.

shadypalm88

Quote from: Joe on December 29, 2005, 10:05 PM
Better way, please?
I already linked to a better way on this thread.
http://misc.ionws.com/binary.lib.phps
<?php

define
('LITTLE_ENDIAN'0);
define('BIG_ENDIAN'1);

// Default byte order
if (!defined('BYTE_ORDER'))
define('BYTE_ORDER'LITTLE_ENDIAN);

function 
parse_word($word)
{
    if (
BYTE_ORDER == BIG_ENDIAN)
        return (
ord($word{0}) << 8) | ord($word{1});
    else
        return (
ord($word{1}) << 8) | ord($word{0});
}

function 
make_word($word)
{
    if (
BYTE_ORDER == BIG_ENDIAN)
        return 
chr(($word 0xFF00) >> 8).chr($word 0x00FF);
    else
        return 
chr($word 0x00FF).chr(($word 0xFF00) >> 8);
}

function 
parse_dword($dword)
{
    if (
BYTE_ORDER == BIG_ENDIAN) {
        return (
ord($dword{0}) << 24) | (ord($dword{1}) << 16) |
            (
ord($dword{2}) << 8) | ord($dword{3});
    } else {
        return (
ord($dword{3}) << 24) | (ord($dword{2}) << 16) |
            (
ord($dword{1}) << 8) | ord($dword{0});
    }
}

function 
make_dword($num)
{
    if (
BYTE_ORDER == BIG_ENDIAN) {
        return 
chr(($num 0xFF000000) >> 24).chr(($num 0x00FF0000) >> 16).
            
chr(($num 0x0000FF00) >> 8).chr($num 0x000000FF);
    } else {
        return 
chr($num 0x000000FF).chr(($num 0x0000FF00) >> 8).
            
chr(($num 0x00FF0000) >> 16).chr(($num 0xFF000000) >> 24);
    }
}

?>