Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

288
Visualizações
Javascript hexadecimal to ASCII with latin extended symbols

I am getting a hexadecimal value of my string that looks like this:

String has letters with diacritics: č,š,ř, ...

Hexadecimal value of this string is:

0053007400720069006E006700200068006100730020006C0065007400740065007200730020007700690074006800200064006900610063007200690074006900630073003A0020010D002C00200161002C00200159002C0020002E002E002E

The problem is that when i try to convert this value back to ascii it poorly converts the č,š,ř,.. and returns symbol of little box with question mark in it instead of these symbols.

My code for converting hex to ascii:

function convertHexadecimal(hexx){

  let index = hexx.indexOf("~");
  let strInfo = hexx.substring(0, index+1);
  let strMessage = hexx.substring(index+1); 
  var hex  = strMessage.toString();
  var str = '';     
  for (var i = 0; i < hex.length; i += 2){     
      str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));     
  }
  console.log("Zpráva: " + str);
  var strFinal = strInfo + str;
  return strFinal; 
}

Can somebody help me with this?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

First an example solution:

let demoHex = `0053007400720069006E006700200068006100730020006C0065007400740065007200730020007700690074006800200064006900610063007200690074006900630073003A0020010D002C00200161002C00200159002C0020002E002E002E`;

function hexToString(hex) {
    let str="";
    for( var i = 0; i < hex.length; i +=4) {
       str += String.fromCharCode( Number("0x" + hex.substr(i,4)));
    }
    return str;
}
console.log("Decoded string: %s", hexToString(demoHex) );

What it's doing:

It's treating the hex characters as a sequence of 4 hexadecimal digits that provide the UTF-16 character code of a character.

  • It gets each set of 4 digits in a loop using String.prototype.substr. Note MDN says .substr is deprecated but this is not mentioned in the ECMASript standard - rewrite it to use substring or something else as you wish.

  • Hex characters are prefixed with "0x" to make them a valid number representation in JavaScript and converted to a number object using Number. The number is then converted to a character string using the String.fromCharCode static method.

I guessed the format of the hex string by looking at it, which means a general purpose encoding routine to encode UTF16 characters (not code points) into hex could look like:

const hexEncodeUTF16 =
   str=>str.split('')
  .map( char => char.charCodeAt(0).toString(16).padStart(4,'0'))
  .join('');

console.log( hexEncodeUTF16( "String has letters with diacritics: č, š, ř, ..."));

I hope these examples show what needs doing - there are any number of ways to implement it in code.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda