From: "Ara.T.Howard" Date: 2005-07-18T22:19:59+09:00 Subject: Re: Bitwise Shift Porting from PHP --8323328-1689967568-1121692797=:7823 Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1689967568-1121692797=:7823" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --8323328-1689967568-1121692797=:7823 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE On Mon, 18 Jul 2005, Dan Fitzpatrick wrote: > I am trying to port the OLE Reader from PHP to read Excel files on any=20 > 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=20 > data[pos,0] is the character =FE, the function returns -2 in PHP and 4294= 967294=20 > 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 =3D> 4294967296. for larger numbers your php function wr= aps and yields negative numbers - dunno if it's supposed to. you should look a= t 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 reg= ard to your data and these functions. hth. -a --=20 =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D --8323328-1689967568-1121692797=:7823-- --8323328-1689967568-1121692797=:7823--