Home > Software engineering >  Proper hexadecimal conversion to text/ascii
Proper hexadecimal conversion to text/ascii

Time:08-01

Trying to figure out how to get my qr scanners to output text to be used in my website. My qr reader is outputting text that needs to be rendered with event.target.value.getuintsomeintegerhere. I cannot get this to convert to text unless I plop it into some hex decoder online.

I do notice the comment about using TextDecoder to process the data, but it still sort of takes me back to the same place off not really understanding what's going on with this sort of thing...

For example

0x6d 0x6f 0x6f 0x0d should read 'moo'

I understand that the encoding is "utf-8" but not what that means or how to modify the code so that it outputs things correctly...

Here's a chunk of the code that seems to be where i need the most help:

function handleNotifications(event) {
  let value = event.target.value;
  let a = [];
  // Convert raw data bytes to hex values just for the sake of showing something.
  // In the "real" world, you'd use data.getUint8, data.getUint16 or even
  // TextDecoder to process raw data bytes.
  for (let i = 0; i < value.byteLength; i  ) {
    a.push('0x'   ('00'   value.getUint8(i).toString(16)).slice(-2));
  }
  log(hex2ascii('> '   a.join(' ')));
}

CodePudding user response:

function handleNotifications(event) {
  let value = event.target.value;
  let a = [];
  // Convert raw data bytes to hex values just for the sake of showing something.
  // In the "real" world, you'd use data.getUint8, data.getUint16 or even
  // TextDecoder to process raw data bytes.
  for (let i = 0; i < value.byteLength; i  ) {
    a.push('0x'   ('00'   value.getUint8(i).toString(16)).slice(-2));
  }
  log(hex2ascii('> '   a.join(' ')));
}

function hex2ascii(hexString) {
  const numberArray =  hexString.slice(2) // Get the string without the first two characters (removes '> ')
    .split(' ') // Split the string on spaces.
    .map(hex => Number(hex)); // Create a new array where each element of the hex array is converted to a JavaScript Number
  
  // Now we have an array of Numbers.
  
  const byteArray = new Uint8Array(numberArray); // Make an array of bytes from the number array This is necessary to use the TextDecoder.
  
  const string = (new TextDecoder()).decode(byteArray); // Create a TextDecoder, and decode the byte array to get a string.
  
  return string;
}

console.log(hex2ascii("> 0x6d 0x6f 0x6f 0x0d"));

CodePudding user response:

It can all be vastly simplified

const h2a = str => str && str.replace(/ ?0x/g,"") // get rid of the space and 0x
  .match(/[a-zA-Z0-9]{1,2}/g) // get the 00-FF parts
  .map(v => String.fromCharCode(parseInt(v, 16))) // convert each to in using hex radix and get the ascii from the int
  .join(''); // string them together
document.getElementById("hex")
  .addEventListener("input", e => document.getElementById("ascii").value = h2a(e.target.value))
<input type="text" value="" id="hex" /> Hex (paste or scan - for example cut and paste 0x6d 0x6f 0x6f 0x0d)
<hr/>
<input type="text" value="" id="ascii" /> Ascii

  • Related