开发者

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜