Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a char from an alphabetical character to a hexadecimal number in Java?

Tags:

How do I convert a char from an alphabetical character to hexadecimal number in Java? If any one knows any built-in methods in Java that does the job or if you have your own method, could you please help?

Also, how would I convert from hex to binary?

like image 556
Cooper Avatar asked Dec 18 '10 10:12

Cooper


People also ask

How do you write hexadecimal in java?

In Java code (as in many programming languages), hexadecimal nubmers are written by placing 0x before them. For example, 0x100 means 'the hexadecimal number 100' (=256 in decimal). Decimal values of hexadecimal digits.


1 Answers

You can convert from char to hex string.

char ch =  String hex = String.format("%04x", (int) ch); 

To read hex and convert to binary you can do

int num = Integer.parseInt(text, 16); String bin = Integer.toString(num, 2); 
like image 170
Peter Lawrey Avatar answered Sep 26 '22 20:09

Peter Lawrey