# Reading unsigned byte

**URL:** <https://www.ruby-forum.com/t/reading-unsigned-byte/88023>\
**Category:** Ruby\
**Created:** [March 7, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/reading-unsigned-byte/88023 "2007-03-07T16:44:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![warhero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8baadc/32.png) [@warhero](https://www.ruby-forum.com/u/warhero)\
**Post date:** [March 7, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/reading-unsigned-byte/88023/1 "2007-03-07T16:44:35Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![warhero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8baadc/32.png) [@warhero](https://www.ruby-forum.com/u/warhero)\
**Post date:** [March 7, 2007, 5:06pm UTC](https://www.ruby-forum.com/t/reading-unsigned-byte/88023/2 "2007-03-07T17:06:03Z")

</div>

On Mar 7, 9:44 am, warhero [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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

---

<div class="post-metadata">

**Author:** ![warhero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/8baadc/32.png) [@warhero](https://www.ruby-forum.com/u/warhero)\
**Post date:** [March 7, 2007, 5:20pm UTC](https://www.ruby-forum.com/t/reading-unsigned-byte/88023/3 "2007-03-07T17:20:58Z")

</div>

Dale M. wrote:

> On Mar 7, 9:44 am, warhero [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > 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

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