This is a discussion on BigEndian to integer within the PHP General forums, part of the PHP Programming Forums category; A bit off topic Im trying to figure out how to convert BigEndian byte words to integers. For example -> ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
A bit off topic
Im trying to figure out how to convert BigEndian byte words to integers. For example -> 0 0 0 18 = 18 The only way I know how to convert this is by writing down on paper, and writing 8 4 2 1 above the numbers and adding up the values. Trying to figure out a way via plain old math/php has me stumped at the moment. (Maybe some sleep is in order) Any help and or tips would be greatly appreciated. Thanks |
|
|||
|
Hello,
This is a reply to an e-mail that you wrote on Mon, 14 Jul 2003 at 09:00, lines prefixed by '>' were originally written by you. > A bit off topic > Im trying to figure out how to convert BigEndian byte words to > integers. > For example -> > 0 0 0 18 = 18 > The only way I know how to convert this is by writing down on paper, > and > writing 8 4 2 1 above the numbers and adding up the values. Never heard of a BigEndian number beofre, but this should do it... function BigEndiantoInt($bigendian){ $bits = split(" ",$bigendian); $bits = array_reverse($bits); $value = 0; $columnvalue = 1; foreach($bits as $thisbit){ $value = $value + ($columnvalue * $thisbit); $columnvalue = $columnvalue * 2; } return $value; } HTH David -- phpmachine :: The quick and easy to use service providing you with professionally developed PHP scripts :: http://www.phpmachine.com/ Professional Web Development by David Nicholson http://www.djnicholson.com/ QuizSender.com - How well do your friends actually know you? http://www.quizsender.com/ (developed entirely in PHP) |
|
|||
|
> Marek Kilimajer wrote:
> http://www.php.net/pack A little background on what Im doing. Im attempting to read realaudio files for their metadata, so Im reading from a binary string, and Im currently using unpack() (Im new to using this function, so I may be wrong with its usage) to unpack the data, along with ord(), and the result is something like what I posted before -> 0 0 0 18 (I put the spaces in there so we know that each number is a byte) So Im not exactly sure how the pack() function would play a role in this. David Nicholson wrote: >Never heard of a BigEndian number beofre, but this should do it... > >function BigEndiantoInt($bigendian){ > $bits = split(" ",$bigendian); > $bits = array_reverse($bits); > $value = 0; $columnvalue = 1; > foreach($bits as $thisbit){ > $value = $value + ($columnvalue * $thisbit); > $columnvalue = $columnvalue * 2; > } > return $value; >} > For taste of what BigEndian or its counterpart LittleEndian is about, look at -> http://www.webopedia.com/TERM/B/big_endian.html http://whatis.techtarget.com/definit...211659,00.html I'll give your function a shot, and see if I can get something meaningful from it. Thanks. |
|
|||
|
Hi,
Monday, July 14, 2003, 6:00:04 PM, you wrote: GS> A bit off topic GS> Im trying to figure out how to convert BigEndian byte words to integers. For example ->> GS> 0 0 0 18 = 18 GS> The only way I know how to convert this is by writing down on paper, and GS> writing 8 4 2 1 above the numbers and adding up the values. GS> Trying to figure out a way via plain old math/php has me stumped at the GS> moment. (Maybe some sleep is in order) GS> Any help and or tips would be greatly appreciated. GS> Thanks Here is a simple class that should manipulate bigendian stuff (untested) <?php class bigEndian { var $data //pointer to data var $ptr //current position in data function bigEndian(&$data){ $this->data =& $data; $this->ptr = 0; } function getChar(){ $r = ord($this->data[$this->ptr]); $this->ptr ++; return $r; } function getWord(){ $r = (ord($this->data[$this->ptr]) << 8) | ord($this->data[$this->ptr+1]); $this->ptr += 2; return $r; } function getDoubleWord(){ $r = (ord($this->data[$this->ptr]) << 24) | (ord($this->data[$this->ptr+1]) << 16) | (ord($this->data[$this->ptr+2]) << 8) | ord($this->data[$this->ptr+3]); $this->ptr += 4; return $r; } } //usage $datablock = file_get_contents($filename); $be = new bigEndian($datablock); $firstword = $be->getWord() //bytes 0,1 $char = $be->getChar() //byte 2 $doubleword = $be->getDoubleWord() //bytes 3,4,5,6 .. .. ?> -- regards, Tom |
|
|||
|
Hi,
Tuesday, July 15, 2003, 7:00:33 PM, you wrote: >> > http://www.php.net/pack >> >> A little background on what Im doing. Im attempting to read realaudio >> files for their metadata, so Im reading from a binary string, >> and Im currently using unpack() (Im new to using this function, so I may >> be wrong with its usage) to unpack the data, along with ord(), >> and the result is something like what I posted before -> 0 0 0 18 (I >> put the spaces in there so we know that each number is a byte) >> So Im not exactly sure how the pack() function would play a role in this. JR> Just in case you're still groggy when you wake up this morning, the page JR> for pack() lists an 'N' format for unsigned long big endian byte order JR> for pack() and unpack(). I think what you probably want to do most is JR> use the 'N' format when you unpack. JR> (The other responses were amusing, was it a full moon last night? ;-)) JR> -- JR> Joel Rees, permanently jetlagged programmer, Kansai Systems Group JR> Altech Corporation (Alpsgiken), Osaka, Japan JR> http://www.alpsgiken.co.jp Trying to use unpack by itself on large binary streams with variable packet sizes and content buried in them can become a nightmare. Try to unscramble an excel file using unpack() :) -- regards, Tom |
|
|||
|
Joel Rees wrote:
>Just in case you're still groggy when you wake up this morning, the page >for pack() lists an 'N' format for unsigned long big endian byte order >for pack() and unpack(). I think what you probably want to do most is >use the 'N' format when you unpack. > >(The other responses were amusing, was it a full moon last night? ;-)) > Thanks for the heads up. Ill give it a try... |
|
|||
|
Tom Rogers wrote:
>Trying to use unpack by itself on large binary streams with variable packet >sizes and content buried in them can become a nightmare. Try to >unscramble an excel file using unpack() :) > Yeah, not exactly the most exciting thing to do. As long as I have some specs on the format, its hasn't been too bad with mp3 and realaudio files so far... |