Hex Number to Char using Javascript

How can I convert, p.ex, the string "C3" to a char into it using javascript? I tried charCodeAt, toString (16) and everything does not work.

var justtesting= "C3"; //there an input here
var tohexformat= '\x' + justtesting; //gives wrong hex number

var finalstring= tohexformat.toString(16); 

thank

+4
source share
1 answer

All you need, parseIntand possibly String.fromCharCode.

parseInt takes a string and radix, aka the base from which you want to convert.

console.log(parseInt('F', 16));
Run codeHide result

String.fromCharCode will accept the character code and convert it to the appropriate line.

console.log(String.fromCharCode(65));
Run codeHide result

So how can you convert C3to a number and, optionally, to a character.

var input = 'C3';
var decimalValue = parseInt(input, 16); // Base 16 or hexadecimal
var character = String.fromCharCode(decimalValue);
console.log('Input:', input);
console.log('Decimal value:', decimalValue);
console.log('Character representation:', character);
Run codeHide result
+4
source

Source: https://habr.com/ru/post/1662856/


All Articles