parsing data and displaying hex

  • Thread starter Ray Pendergraph
  • Start date
R

Ray Pendergraph

I am sort of a novice to Ruby so I will just start out with a
simplified example what I want to do. I have an array with two bytes
in it [0x03,0x9A] and depending on what the byte order of the file
(not the architecture) is want to display these bytes as 922 or 39427
(both unsigned shorts). I want to do this without reversing the index
on the array or anything goofy like that. Whats the most proper way to
do this in Ruby? I suspect my solution lies somewhere in pack... I
toyed with it briefly but decided to ask.
 
T

Tim Hunter

Ray said:
I am sort of a novice to Ruby so I will just start out with a
simplified example what I want to do. I have an array with two bytes
in it [0x03,0x9A] and depending on what the byte order of the file
(not the architecture) is want to display these bytes as 922 or 39427
(both unsigned shorts). I want to do this without reversing the index
on the array or anything goofy like that. Whats the most proper way to
do this in Ruby? I suspect my solution lies somewhere in pack... I
toyed with it briefly but decided to ask.

I don' t know if this is goofy or not, but here goes:

irb(main):005:0> a = [0x03,0x9a]
=> [3, 154]
irb(main):006:0> p (a[0]<<8)+a[1]
922
=> nil
irb(main):007:0> p (a[1]<<8)+a[0]
39427
=> nil
irb(main):008:0>
 
M

Mark Hubbart

I am sort of a novice to Ruby so I will just start out with a
simplified example what I want to do. I have an array with two bytes
in it [0x03,0x9A] and depending on what the byte order of the file
(not the architecture) is want to display these bytes as 922 or 39427
(both unsigned shorts). I want to do this without reversing the index
on the array or anything goofy like that. Whats the most proper way to
do this in Ruby? I suspect my solution lies somewhere in pack... I
toyed with it briefly but decided to ask.

unpack is easier than it first looks...
Assuming unsigned integers:

# pack the bytes
bytes = [0x03,0x9A].pack('cc')
# unpack in big-endian
big = bytes.unpack('n')
# unpack small-endian
small = bytes.unpack('v')

I'm not sure if there's a way to use pack for this using signed
integers. I didn't see anything in the reference that offers unpacking
two bytes as signed little-endian integers. If you need signed
integers, you might have to resort to tricks.

reference for string.unpack:
http://www.rubycentral.com/ref/ref_c_string.html#unpack

hth,
Mark
 

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

Similar Threads

Parsing String Data 4
Parsing text 3
parse hex ascii 3
Parsing Numeric Data 2
convert byte array to hex string using BigInteger 21
Pdf Parsing Project Example 4
pack and hex 9
Byte–stream parsing in Ruby 13

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top