如何在Java中将字符从字母字符转换为十六进制数字?

2022-01-12 00:00:00 char methods hex java

如何在 Java 中将字符从字母字符转换为十六进制数字?如果有人知道 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 does the job or if you have your own method, could you please help?

另外,如何将十六进制转换为二进制?

Also, how would I convert from hex to binary?

推荐答案

你可以从char转换成hex字符串.

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);

相关文章