Compress device ID

I know device ID is a 24 hex digits in a string. I want to compress it to 6-char string using the following algorithm.

Since String class on Electron has no fromCharCode function and no parseInt function is available either, how can I implement the same algorithm?


function hexToString(hex) {
  var string = ""

  for (var i = 0; i < hex.length; i+= 4){
    string += String.fromCharCode(parseInt(hex.substring(i,i + 4), 16)) // get char from ascii code which goes from 0 to 65536

  return string;

Since two hex digits comprise one byte I’m not sure how you want to compress 12 bytes into a 6 character string :confused:
ASCII is a 8 byte code (0…255) :wink:

However, have you considered using strtoul() or sscanf() for the conversion?

1 Like

Yes, strtol works. You are right that I cannot compress the device ID to 6 character string but 12 character string is fine. Thanks.

1 Like