Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Convert a raw string to an array of big-endian words

Reply
Thread Tools

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

 
 
Paul A.
Guest
Posts: n/a
 
      05-02-2010
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 +=
output[i>>5] |= (input.charCodeAt(i / & 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/.

 
Reply With Quote
 
 
 
 
Paul A.
Guest
Posts: n/a
 
      05-02-2010
Done! We can do it with:

>> str.unpack('N*')


Regards
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Replace stop words (remove words from a string) BerlinBrown Python 6 01-17-2008 02:37 PM
How to convert Unicode string to raw string escaped with HTML Entities ldng Python 3 05-10-2007 09:37 AM
split camelcase string into array of words words pantagruel Javascript 8 07-22-2006 07:46 PM
Non-noise words are incorrectly recognised as noise words. Peter Strĝiman ASP .Net 1 08-23-2005 01:26 PM
convert string to raw string? Phd Python 3 12-06-2004 04:36 PM



Advertisments