reading unsigned byte

W

warhero

I'm experiencing unexpected behavior when reading an byte.. as apposed
to PHP which is returning expected results.

this is how I'm reading the byte in ruby: int =
somedata.unpack('c').first

the result I'm getting in ruby is -127. In php I get 128. This is how I
read it in php:

int = ord(@this->raw_data($this->current_byte++]);

not sure how to go about debugging this..
thanks
 
D

Dale Martenson

I'm experiencing unexpected behavior when reading an byte.. as apposed
to PHP which is returning expected results.

this is how I'm reading the byte in ruby: int =
somedata.unpack('c').first

the result I'm getting in ruby is -127. In php I get 128. This is how I
read it in php:

int = ord(@this->raw_data($this->current_byte++]);

not sure how to go about debugging this..

'c' extracts a signed integer. 'C' extracts an unsigned integer.

irb(main):002:0> "\x80".unpack('c').first
=> -128
irb(main):003:0> "\x80".unpack('C').first
=> 128
 
A

Aaron Smith

Dale said:
'c' extracts a signed integer. 'C' extracts an unsigned integer.

irb(main):002:0> "\x80".unpack('c').first
=> -128
irb(main):003:0> "\x80".unpack('C').first
=> 128

Ha, Duh, as soon as you posted this I caught that in the docs. Thanks.
 

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

Forum statistics

Threads
474,236
Messages
2,571,185
Members
47,820
Latest member
HortenseKo

Latest Threads

Top