简体   繁体   中英

How to convert Javascript string to octet/character array?

I would like to know how to convert a normal Javascript string into an array of octets/characters. Like you would do with a classic C unsigned char array. I am using the Struct/JSPack library and need to extract/unpack some values from data coming in a string.

Thanks!

See String.charCodeAt .

var str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
var result = [];
for(var i = 0, length = str.length; i < length; i++) {
    var code = str.charCodeAt(i);
    // Since charCodeAt returns between 0~65536, simply save every character as 2-bytes
    result.push(code & 0xff00, code & 0xff);
}
alert(result);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM