Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hex Number to Char using Javascript

Tags:

javascript

hex

How do i convert , p.ex, the string "C3" to it's char using javascript? i've tried charCodeAt, toString(16) and everything, doesn't work.

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

var finalstring= tohexformat.toString(16); 

Thank you

like image 847
i.Dio Avatar asked Dec 05 '16 14:12

i.Dio


1 Answers

All you need is parseInt and possibly String.fromCharCode.

parseInt accepts a string and a radix, a.k.a the base you wish to convert from.

console.log(parseInt('F', 16));

String.fromCharCode will take a character code and convert it to the matching string.

console.log(String.fromCharCode(65));

So here's how you can convert C3 into a number and, optionally, into 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);
like image 181
Mike Cluck Avatar answered Nov 09 '22 01:11

Mike Cluck