Re: BigEndian to integer

From: Date: Tue, 15 Jul 2003 00:59:16 +0000
Subject: Re: BigEndian to integer
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-155205@lists.php.net to get a copy of this message
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

« previous php.general (#155205) next »