Go Back   Forum Care Forums > Development Reference Area > Php Development

Reply
 
LinkBack Thread Tools Display Modes
Best way to convert Little-Endian DWORD to string
Old
  (#1)
Tijnema
Guest
 
Posts: n/a
Default Best way to convert Little-Endian DWORD to string - 06-02-2007, 08:56 PM

Hi,

Maybe quite strange question and quite off topic, but there's PHP involved

I want to read out files, and then I come across 4byte DWORD values,
they are stored in little endian values, but how do I get in a PHP
string (as the values are too large for integer)

My code is currently this:
$data = fread($fp,4);
$value = (ord(substr($data,3,1))*256*256*256)+(ord(substr($ data,2,1))*256*256)+(ord(substr($data,1,1))*256)+( ord(substr($data,0,1)));

and now $value contains the value of the DWORD, it works fine, but it
seems that it could be done a lot easier
Anyone experience with it?

Tijnema
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

RE: [PHP] Best way to convert Little-Endian DWORD to string
Old
  (#2)
Edward Kay
Guest
 
Posts: n/a
Default RE: [PHP] Best way to convert Little-Endian DWORD to string - 06-02-2007, 08:56 PM



> -----Original Message-----
> From: Tijnema [private.php?do=newpm&u=]
> Sent: 30 May 2007 15:20
> To: php
> Subject: [PHP] Best way to convert Little-Endian DWORD to string
>
>
> Hi,
>
> Maybe quite strange question and quite off topic, but there's PHP
> involved
>
> I want to read out files, and then I come across 4byte DWORD values,
> they are stored in little endian values, but how do I get in a PHP
> string (as the values are too large for integer)
>
> My code is currently this:
> $data = fread($fp,4);
> $value =
> (ord(substr($data,3,1))*256*256*256)+(ord(substr($ data,2,1))*256*2
> 56)+(ord(substr($data,1,1))*256)+(ord(substr($data ,0,1)));
>
> and now $value contains the value of the DWORD, it works fine, but it
> seems that it could be done a lot easier
> Anyone experience with it?


Could you not use the << bitwise operator?

Edward
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] Best way to convert Little-Endian DWORD to string
Old
  (#3)
Tijnema
Guest
 
Posts: n/a
Default Re: [PHP] Best way to convert Little-Endian DWORD to string - 06-02-2007, 08:56 PM

On 5/30/07, Edward Kay <EMAIL REMOVED> wrote:
>
>
> > -----Original Message-----
> > From: Tijnema [private.php?do=newpm&u=]
> > Sent: 30 May 2007 15:20
> > To: php
> > Subject: [PHP] Best way to convert Little-Endian DWORD to string
> >
> >
> > Hi,
> >
> > Maybe quite strange question and quite off topic, but there's PHP
> > involved
> >
> > I want to read out files, and then I come across 4byte DWORD values,
> > they are stored in little endian values, but how do I get in a PHP
> > string (as the values are too large for integer)
> >
> > My code is currently this:
> > $data = fread($fp,4);
> > $value =
> > (ord(substr($data,3,1))*256*256*256)+(ord(substr($ data,2,1))*256*2
> > 56)+(ord(substr($data,1,1))*256)+(ord(substr($data ,0,1)));
> >
> > and now $value contains the value of the DWORD, it works fine, but it
> > seems that it could be done a lot easier
> > Anyone experience with it?

>
> Could you not use the << bitwise operator?
>
> Edward


How could I use them here?
I read the docs for bitwise operators, and they tell me what they do,
but now how to use it

Tijnema
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: [PHP] Best way to convert Little-Endian DWORD to string
Old
  (#4)
Richard Lynch
Guest
 
Posts: n/a
Default Re: [PHP] Best way to convert Little-Endian DWORD to string - 06-02-2007, 08:56 PM

On Wed, May 30, 2007 9:19 am, Tijnema wrote:
> Maybe quite strange question and quite off topic, but there's PHP
> involved
>
> I want to read out files, and then I come across 4byte DWORD values,
> they are stored in little endian values, but how do I get in a PHP
> string (as the values are too large for integer)
>
> My code is currently this:
> $data = fread($fp,4);
> $value =
> (ord(substr($data,3,1))*256*256*256)+(ord(substr($ data,2,1))*256*256)+(ord(substr($data,1,1))*256)+( ord(substr($data,0,1)));
>
> and now $value contains the value of the DWORD, it works fine, but it
> seems that it could be done a lot easier
> Anyone experience with it?


Easiest might be to read one byte at a time...

$byte0 = fread($fp, 1);
$byte1 = fread($fp, 1);
$byte2 = fread($fp, 1);
$byte3 = fread($fp, 1);
$value = $byte3 * 0xffffff + $byte2 * 0xffff + $byte1 * 0xff + $byte0;

If that makes fread too slow, which it might, but buffering should be
able to handle 4 bytes...

You could also do:
$byte0 = 0xff000000 & $data;
$byte1 = 0x00ff0000 & $data;
$byte2 = 0x0000ff00 & $data;
$byte3 = 0x000000ff & $data;
//same $value line as above

As far as shifting goes... I *think* this would do it:

$byte3 = $data >> 24;
$byte2 = ($data >> 16) & 0xff;
$byte1 = ($data >> 8) & 0xff;
$byte0 = $data & 0xff;

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top