0

I'm making a tool that converts unicode value to character with text input and javascript.

Here's what I've done so far, I am quite new to Javascript so I suppose the code looks quite odd:

setInterval(
function tochar() {
  const hex = +document.getElementById('hex').value;
  const char = hex.toString();
  document.getElementById("char").value = char;
});
unicode <input type="text" id="hex" value="\uac00"/>
➜char <input type="text" id="char"/>

I want to make the right input to render '가', which is the character value for \uac00. What edits should I make? I have already refered to a lot of questions and answers on StackOverflow and other sources, but could not find something helpful for my purpose.

Henry
  • 154
  • 10
  • https://stackoverflow.com/questions/3835317/unicode-value-uxxxx-to-character-in-javascript – epascarello Jan 06 '21 at 17:17
  • 1
    This: `const char = hex.toString();` just sets `char` to exactly the same string that `hex` already is; "hex" is a String already, `toString()` on a String just returns the same string. It seems you're expecting that line to do some kind of conversion (?) – Stephen P Jan 06 '21 at 17:20
  • @epascarello I've seen this one, but it looks like the answers mainly deal with hexadecimals. I'm not sure how to make use of this. – Henry Jan 06 '21 at 17:20
  • 1
    The second answer in that is exactly what you want. – epascarello Jan 06 '21 at 17:26
  • @epascarello just noticed it, thanks. – Henry Jan 06 '21 at 17:26
  • Does this answer your question? [Unicode value \uXXXX to Character in Javascript](https://stackoverflow.com/questions/3835317/unicode-value-uxxxx-to-character-in-javascript) – Stephen P Jan 06 '21 at 19:00

1 Answers1

1

function tochar() {

  // Get input
  let hex = document.getElementById('hex').value;
  
  // Remove \u
  hex = hex.slice(2);
  
  // Convert to char
  const char = String.fromCharCode(parseInt(hex,16));
  
  // Show
  document.getElementById("char").value = char;
};
tochar();
unicode <input type="text" id="hex" value="\uac00"/>
➜char <input type="text" id="char"/>
0stone0
  • 34,288
  • 4
  • 39
  • 64