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

**URL:** <https://www.ruby-forum.com/t/convert-a-raw-string-to-an-array-of-big-endian-words/187931>\
**Category:** Ruby\
**Created:** [May 2, 2010, 1:57pm UTC](https://www.ruby-forum.com/t/convert-a-raw-string-to-an-array-of-big-endian-words/187931 "2010-05-02T13:57:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [May 2, 2010, 1:57pm UTC](https://www.ruby-forum.com/t/convert-a-raw-string-to-an-array-of-big-endian-words/187931/1 "2010-05-02T13:57:35Z")

</div>

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[i] = 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

---

<div class="post-metadata">

**Author:** ![judge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/9de0a6/32.png) [@judge](https://www.ruby-forum.com/u/judge)\
**Post date:** [May 2, 2010, 3:41pm UTC](https://www.ruby-forum.com/t/convert-a-raw-string-to-an-array-of-big-endian-words/187931/2 "2010-05-02T15:41:49Z")

</div>

Done! We can do it with:

> > str.unpack(‘N\*’)

Regards
