Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting binary to hexadecimal

Tags:

javascript

I am converting binary to hexadecimal but the code below returns a wrong answer:

var number = 1011;
var hexa = parseInt(number, 2).toString(16);
return hexa;

This returns b but it should have to be return B. What is the problem?

like image 871
Go Go lu Avatar asked Apr 12 '16 03:04

Go Go lu


2 Answers

'b' is correct. Hexadecimal doesn't specify letter case, and many write hex strings with lower-case letters.

like image 68
rsjaffe Avatar answered Oct 23 '22 01:10

rsjaffe


Just add toUpperCase():

var hexa = parseInt(number, 2).toString(16).toUpperCase();
like image 12
Lukasz Czerwinski Avatar answered Oct 23 '22 00:10

Lukasz Czerwinski