How to convert a char from an alphabetical character to a hexadecimal number in Java?
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 开发者_StackOverflow中文版does the job or if you have your own method, could you please help?
Also, how would I convert from hex to binary?
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);
You could use:
Integer.toHexString((int) 'a');
Integer.toBinaryString((int) 'b');
Update: hex -> binary conversion:
Integer.toBinaryString(Integer.parseInt("fa", 16))
Use the apache commons codec library
Specifically: http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Hex.html
精彩评论