Character representation from hexadecimal

20,531

Solution 1

Not in that fashion, because JavaScript is loosely typed, and does not allow one to define a variable's data type.

What you can do, though, is creating a shortcut:

var char = String.fromCharCode; // copy the function into another variable

Then you can call char instead of String.fromCharCode:

char(0x61); // a

Which is quite close to what you want (and perhaps more readable/requiring less typing).

Solution 2

You can use the \xNN notation:

var str = "\x61";

Solution 3

There is also the Unicode equivalent of \x:

var char = "\u0061";
Share:
20,531
The Mask
Author by

The Mask

Updated on July 05, 2022

Comments

  • The Mask
    The Mask almost 2 years

    Is it possible to convert a hexadecimal value to its respective ASCII character, not using the String.fromCharCode method, in JavaScript?

    For example:

    JavaScript:

    0x61 // 97 
    String.fromCharCode(0x61) // a
    

    C-like:

    (char)0x61 // a 
    
  • NoodleOfDeath
    NoodleOfDeath about 5 years
    Can you do something like var str = "\x61\x62\x61\x41" to get the string "abaA" ?