Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Character representation from hexadecimal

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 
like image 841
The Mask Avatar asked Oct 12 '11 20:10

The Mask


3 Answers

There is also the Unicode equivalent of \x:

var char = "\u0061";
like image 91
david Avatar answered Oct 02 '22 19:10

david


You can use the \xNN notation:

var str = "\x61";
like image 21
rid Avatar answered Oct 20 '22 00:10

rid


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).

like image 17
pimvdb Avatar answered Oct 19 '22 23:10

pimvdb