From: Mark Hubbart Date: 2004-10-09T03:27:21+09:00 Subject: Re: parsing data and displaying hex On Fri, 8 Oct 2004 23:04:44 +0900, Ray Pendergraph wrote: > 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