From: Dan Fitzpatrick Date: 2005-07-18T23:37:28+09:00 Subject: Re: Bitwise Shift Porting from PHP Ara.T.Howard wrote: > On Mon, 18 Jul 2005, Dan Fitzpatrick wrote: > >> I am trying to port the OLE Reader from PHP to read Excel files on any >> platform. I am stuck on the following function: >> >> function GetInt4d($data, $pos) { >> return ord($data[$pos]) | (ord($data[$pos+1]) << 8) | \ >> (ord($data[$pos+2]) << 16) | (ord($data[$pos+3]) << 24); >> } >> >> My ruby version is: >> >> def get_int_4_d(data, pos) >> (data[pos]) | ((data[pos+1]) << 8) | \ >> ((data[pos+2]) << 16) | ((data[pos+3]) << 24) >> end >> >> It works in some cases but in the example xls file I am using, when >> data[pos,0] is the character �, the function returns -2 in PHP and >> 4294967294 in Ruby. >> >> Thanks for your help. > > > your php function should have been named > > GetSignedInt > > and your ruby function should be named > > get_unsigned_int_4_d > > remember, 2 ** 32 => 4294967296. for larger numbers your php function > wraps > and yields negative numbers - dunno if it's supposed to. you should > look at > String#unpack. assuming you want to unpack a signed int in lsb order from > data starting at pos your method would become > > def get_int_4_d(data, pos) > data[pos,4].unpack('i').first > end > > although you should be sure to understand little/big endian issues with > regard > to your data and these functions. > > hth. > > -a Ara, Thanks. That works great. I will look into endian info. Don't know anything about it yet. Dan