Bitwise Shift Porting from PHP

D

Dan Fitzpatrick

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=20
4294967294 in Ruby.

Thanks for your help.

Dan
 
A

Ara.T.Howard

--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

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--
 
D

Dan Fitzpatrick

Ara.T.Howard said:
On Mon, 18 Jul 2005, Dan Fitzpatrick wrote:
=20
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 =EF=BF=BD, the function returns -2 in PHP= and=20
4294967294 in Ruby.

Thanks for your help.
=20
=20
your php function should have been named
=20
GetSignedInt
=20
and your ruby function should be named
=20
get_unsigned_int_4_d
=20
remember, 2 ** 32 =3D> 4294967296. for larger numbers your php functio= n=20
wraps
and yields negative numbers - dunno if it's supposed to. you should=20
look at
String#unpack. assuming you want to unpack a signed int in lsb order f= rom
data starting at pos your method would become
=20
def get_int_4_d(data, pos)
data[pos,4].unpack('i').first
end
=20
although you should be sure to understand little/big endian issues with= =20
regard
to your data and these functions.
=20
hth.
=20
-a

Ara,

Thanks. That works great. I will look into endian info. Don't know=20
anything about it yet.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top