Valhalla Legends Archive

Programming => Web Development => Topic started by: Joe[x86] on December 14, 2005, 09:27 PM

Title: [PHP] Make/Get Int16/Int32 (DWORD/WORD)
Post by: Joe[x86] on December 14, 2005, 09:27 PM
Can anyone post these functions? I can't get it right for the life of me. =(
Title: Re: Resolved: [PHP] Make/Get Int16/Int32 (DWORD/WORD)
Post by: Joe[x86] on December 14, 2005, 09:48 PM
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;
  }
Title: Re: [PHP] Make/Get Int16/Int32 (DWORD/WORD)
Post by: Joe[x86] on December 15, 2005, 03:54 AM
MakeDWORD works correctly but GetDWORD doesn't. Anyone want to take a stab at it?
Title: Re: [PHP] Make/Get Int16/Int32 (DWORD/WORD)
Post by: shadypalm88 on December 17, 2005, 12:13 PM
Binary Data and Packets (http://forum.valhallalegends.com/index.php?topic=11709.0)
Title: Re: [PHP] Make/Get Int16/Int32 (DWORD/WORD)
Post by: Warrior on December 17, 2005, 03:00 PM
Wow Cloaked I forgot you wrote that, very nice.
Title: Re: [PHP] Make/Get Int16/Int32 (DWORD/WORD)
Post by: effect on December 22, 2005, 12:28 AM
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
Title: Re: [PHP] Make/Get Int16/Int32 (DWORD/WORD)
Post by: Joe[x86] on December 22, 2005, 01:50 PM
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));
  }
?>
Title: Re: [PHP] Make/Get Int16/Int32 (DWORD/WORD)
Post by: Warrior on December 22, 2005, 02:43 PM
Not bad you're getting pretty good at PHP :)
Title: Re: [PHP] Make/Get Int16/Int32 (DWORD/WORD)
Post by: shadypalm88 on December 22, 2005, 04:16 PM
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.
Title: Re: [PHP] Make/Get Int16/Int32 (DWORD/WORD)
Post by: 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));
  }
?>


Quote from: shadypalm88 on December 22, 2005, 04:16 PMOh.  My.  God.
Title: Re: [PHP] Make/Get Int16/Int32 (DWORD/WORD)
Post by: Joe[x86] on December 29, 2005, 10:05 PM
Better way, please?
Title: Re: [PHP] Make/Get Int16/Int32 (DWORD/WORD)
Post by: Douglas on January 01, 2006, 06:52 PM
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
Title: Re: [PHP] Make/Get Int16/Int32 (DWORD/WORD)
Post by: shadypalm88 on January 01, 2006, 08:37 PM
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);
   }
}

?>