Search code examples
javascriptdecode

decode from unicode hex to string


When I load my data from my model (asp.net model) if the string is in another language (Russian for example), I'm getting the unicode hex code of the chars. How can I convert them to a normal string? The problem isn't from the javascript encoding, it's from loading it from the model. I tried using a lot of functions from the forum but none worked! Here is an example of a value I should get:

Петър

And here is what I'm actually getting:

Петър

Solution

  • const sequence = 'Петър'
    const charCode = sequence.split(/[;\s]+/g)
      // remove empty
      .filter((v) => v)
      // &#x41F -> 0x41F -> number
      .map((v) => Number.parseInt(v.replace(/&#/, '0')))
    
    console.log(
      String.fromCharCode(...charCode)
    )