I have two hex values and expect back the value "3007" What pack does is take the array of hexes and formats them into a string (if ASCII representation, then ASCII character, otherwise the raw hex as string). Then I take the string of hexes and unpack them, that is, get the unsigned int representation of those bytes: [0xBF, 0xB].pack(''C*'').unpack(''I'')[0] So I expect back 3007, but the above produces nil. This is what I want to achieve: [0xBF, 0xB].reverse.inject { |s,v| s * 256 + v } => 3007 But I want to use unpack instead of inject. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/4d8e50a7-dc9e-4da9-b8e3-bc4ba4f5c62f%40googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
On 24 June 2013 17:49, John Merlino <stoicism1-YDxpq3io04c@public.gmane.org> wrote:> I have two hex values and expect back the value "3007" > > > What pack does is take the array of hexes and formats them into a string (if > ASCII representation, then ASCII character, otherwise the raw hex as > string). Then I take the string of hexes and unpack them, that is, get the > unsigned int representation of those bytes: > [0xBF, 0xB].pack(''C*'').unpack(''I'')[0] > > So I expect back 3007, but the above produces nil.Do the operation one step at a time in irb and you will see where it goes wrong. Colin -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLtoU7D%3DsP9MC%3DHgemwkmUeospCjTJwWR7SU%2BMsmY6%3D3oA%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
On 24 June 2013 20:59, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> On 24 June 2013 17:49, John Merlino <stoicism1-YDxpq3io04c@public.gmane.org> wrote: >> I have two hex values and expect back the value "3007" >> >> >> What pack does is take the array of hexes and formats them into a string (if >> ASCII representation, then ASCII character, otherwise the raw hex as >> string). Then I take the string of hexes and unpack them, that is, get the >> unsigned int representation of those bytes: >> [0xBF, 0xB].pack(''C*'').unpack(''I'')[0] >> >> So I expect back 3007, but the above produces nil. > > Do the operation one step at a time in irb and you will see where it goes wrong.Hint: unpack(''I'') appears to expect four bytes. Colin> > Colin-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsDYXq%3D6PrentSRX9Z4QWH3jSvypmurgmYhRoBeJvNUHQ%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.