Valhalla Legends Forums Archive | Web Development | [PHP] Make/Get Int16/Int32 (DWORD/WORD)

AuthorMessageTime
JoeTheOdd
Can anyone post these functions? I can't get it right for the life of me. =(
December 15, 2005, 3:27 AM
JoeTheOdd
UGH! Leave it to me to forget to call chr().

These should actually work.

[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);
  }
  /* 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;
  }[/code]
December 15, 2005, 3:48 AM
JoeTheOdd
MakeDWORD works correctly but GetDWORD doesn't. Anyone want to take a stab at it?
December 15, 2005, 9:54 AM
shadypalm88
Binary Data and Packets
December 17, 2005, 6:13 PM
Quarantine
Wow Cloaked I forgot you wrote that, very nice.
December 17, 2005, 9:00 PM
Null
[quote author=Warrior link=topic=13523.msg138210#msg138210 date=1134853238]
Wow Cloaked I forgot you wrote that, very nice.
[/quote]

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
December 22, 2005, 6:28 AM
JoeTheOdd
Yeah, where the hell is my report to moderator button?

EDIT -
I finally got it working, so I might as well post it! =)
[code]<?
  /*
  * 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));
  }
?>[/code]
December 22, 2005, 7:50 PM
Quarantine
Not bad you're getting pretty good at PHP :)
December 22, 2005, 8:43 PM
shadypalm88
[quote author=Joe link=topic=13523.msg138875#msg138875 date=1135281017]
[code]<?
  /* 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));
  }
?>[/code]
[/quote]Oh.  My.  God.
December 22, 2005, 10:16 PM
rabbit

[quote author=Joe link=topic=13523.msg138875#msg138875 date=1135281017]
[code]<?
  /* 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));
  }
?>[/code]
[/quote]

[quote author=shadypalm88 link=topic=13523.msg138892#msg138892 date=1135289789]Oh.  My.  God.
[/quote]
December 23, 2005, 3:35 PM
JoeTheOdd
Better way, please?
December 30, 2005, 4:05 AM
Douglas
[quote author=rabbit link=topic=13523.msg139002#msg139002 date=1135352127]

[quote author=Joe link=topic=13523.msg138875#msg138875 date=1135281017]
[code]<?
  /* 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));
  }
?>[/code]
[/quote]

is that written yourself? [url]http://www.php.net/manual/en/ref.strings.php[/url][/quote]
January 2, 2006, 12:52 AM
shadypalm88
[quote author=Joe link=topic=13523.msg139837#msg139837 date=1135915537]
Better way, please?
[/quote]
I already linked to a better way on this thread.
[url]http://misc.ionws.com/binary.lib.phps[/url]
[code]<?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);
    }
}

?>[/code]
January 2, 2006, 2:37 AM

Search