Convert an 8-bit or 16-bit string to an array of big-endian words - Node.js String

Node.js examples for String:Big endian

Description

Convert an 8-bit or 16-bit string to an array of big-endian words

Demo Code


  var chrsz = 8;// w  w w .  ja v  a2  s. c o  m

// In 8-bit function, characters >255 have their hi-byte silently ignored.
  function str2binb(str) {
    var bin = Array();
    var mask = (1 << chrsz) - 1;
    for (var i = 0; i < str.length * chrsz; i += chrsz)
      bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i % 32);
    return bin;
  }

Related Tutorials