Convert a raw string to an array of big-endian words

Discussion in 'Ruby' started by Paul A., May 2, 2010.

  1. Paul A.

    Paul A. Guest

    Hello,

    I would like to convert a raw string to an array of big-endian words.

    As example, here is a JavaScript function that do it well (by Paul
    Johnston):

    /*
    * Convert a raw string to an array of big-endian words
    * Characters >255 have their high-byte silently ignored.
    */
    function rstr2binb(input)
    {
    var output = Array(input.length >> 2);
    for(var i = 0; i < output.length; i++)
    output = 0;
    for(var i = 0; i < input.length * 8; i += 8)
    output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);
    return output;
    }

    I believe the Ruby equivalent can be String#unpack(format).

    However, I don't know what should be the correct format parameter.

    Thank you for any help.

    Regards
    --
    Posted via http://www.ruby-forum.com/.
     
    Paul A., May 2, 2010
    #1
    1. Advertising

  2. Paul A.

    Paul A. Guest

    Paul A., May 2, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hicham
    Replies:
    2
    Views:
    9,026
    dxcoder
    Jul 2, 2003
  2. Ernst Murnleitner

    float: IEEE, big endian, little endian

    Ernst Murnleitner, Jan 13, 2004, in forum: C++
    Replies:
    0
    Views:
    866
    Ernst Murnleitner
    Jan 13, 2004
  3. invincible

    Little Endian to Big Endian

    invincible, Jun 14, 2005, in forum: C++
    Replies:
    9
    Views:
    14,354
    Old Wolf
    Jun 14, 2005
  4. hicham

    convert from big-endian to little-endian

    hicham, Jun 30, 2003, in forum: C Programming
    Replies:
    0
    Views:
    1,525
    hicham
    Jun 30, 2003
  5. Replies:
    5
    Views:
    352
    Stephen Sprunk
    Aug 31, 2006
Loading...

Share This Page