Question

How to count bits of the string in JavaScript? For example how many bits long is the string 0000xfe-kemZlF4IlEgljDF_4df:1102pwrq7?

Was it helpful?

Solution

The string provided ("0000xfe-kemZlF4IlEgljDF_4df:1102pwrq7") would be:

length * 2 * 8

bits long, or 592 bits.

This is because each char in a string is treated as a 16-bit unsigned value, at least in the most common mainstream implementation. The details of this can probably be discussed, but you mention in the comments that it is for security purposes -

So assuming you are giving ASCII characters (0-127) or UTF-8 (0-255) you can use the TextEncoder object to make sure you provide enough chars to produce 128 bits. Just be careful with Latin-1 chars in UTF-8 as the encoder may project them to the UTF-16 equivalent meaning it will produce 2 bytes for it instead of just one.

If you use a plain JavaScript string to hold ASCII characters you will have half the positions represented as 0's which reduce the security significantly, so an encoding from UTF-16/UCS-2 to ASCII or UTF-8 is required.

To use TextEncoder you simply provide a string representing 16 characters, at this point 256 bits (16x16) but where each char is within the ASCII/UTF-8 value range. After encoding, unless some special chars where used, the binary buffer as typed array should represent 128 bits (16x8).

Example

if (!("TextEncoder" in window)) alert("Sorry, no TextEncoder in this browser...");
else {
  btn.onclick = function() {
  
    var s = txt.value;
    if (s.length !== 16) {
      alert("Need 16 chars. " + (16 - s.length) + " to go...");
      return
    }
  
    var encoder = new TextEncoder("ASCII");  // or use UTF-8
    var bytes = encoder.encode(s);
  
    console.log(bytes);

    if (bytes.byteLength === 16) alert("OK, got 128 bits");
    else alert("Oops, got " + (bytes.byteLength * 8) + " bits.");
  };
}
<label>Enter 16 ASCII chars: <input id=txt maxlength=16></label>
<button id=btn>Convert</button>

An alternative to TextEncoder if using older browsers is to manually iterate over the string and extract and mask each char to build a binary array from that.

OTHER TIPS

Can you copy the string into a buffer and then check the length of the buffer?

var str = ' ... ';

var buf = new Buffer(str);

console.log(buf.length);

If, as you say, you just need to make sure the given value is at least 128 bit, then you're probably passing this string to something that will be converting the string to some byte representation. How the string is converted to bytes depends on how it's encoded.

The sample string you gave us contains ASCII-range characters. If the string is encoded as ASCII, then it's 8 bits per character. If the string was encoded as UTF-8, then it would be 8 bits per character, but if the string could contain larger character values than the sample you provided, then it may be more than 8 bits per character depending on the character. If it's encoded as UTF-16, then each character is a minimum of 16 bits, but could be more depending on the character. If it's encoded as USC-2, then it would always be 16 bits per character.

We don't know where this requirement is coming from and how the system requiring this string uses it. If the system uses a fixed number of bits per character, then this is as straightforward as taking the length of the string and multiplying by the appropriate number. If it's not that straightforward, then you would need to encode the string using the proper encoding, most likely to a byte array, then multiply 8 * the number of bytes to get the number of bits.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top