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


P

Paul A.

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
 
Ad

Advertisements


Similar threads

C
Replies
46
Views
4K
Tim Rentsch
T
K
2
Replies
25
Views
1K
Richard Bos
R
P
Replies
1
Views
2K
Paul
P
S
Replies
37
Views
4K
Joshua Maurice
J
L
Replies
11
Views
2K
luser- -droog
L
W
Replies
3
Views
1K
William McBrine
W
G
Replies
2
Views
231
greenflame
G
W
Replies
0
Views
333
Wincent Colaiuta
W
Top