endianness - How to convert to big endian in ruby -


i have string in little-endian order, hexadecimal-encoded string

000000020597ba1f0cd423b2a3abb0259a54ee5f783077a4ad45fb6200000218 000000008348d1339e6797e2b15e9a3f2fb7da08768e99f02727e4227e02903e 43a42b31511553101a051f3c0000000000000080000000000000000000000000 0000000000000000000000000000000000000000000000000000000080020000 

i'd byteswap each 32-bit chunk little-endian big-endian resulting

020000001fba9705b223d40c25b0aba35fee549aa477307862fb45ad18020000 0000000033d14883e297679e3f9a5eb108dab72ff0998e7622e427273e90027e 312ba443105315513c1f051a0000000080000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000280 

i've tried several approaches haven't goten work. it'd great if can show sample implementation.

cheers.

you can use pack , unpack by:

  • first going decoding hexadecimal
  • then converting 32 bit integers in small endian
  • encoding these integers big endian
  • encoding result in hexadecimal.

in code:

s = "000000020597ba1f0cd4..." [s].pack('h*').unpack('n*').pack('v*').unpack('h*') # => "020000001fba9705b223..." 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -