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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4477714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:54:34  来源:igfitidea点击:

how to convert a char from alphabetical character to hexadecimal number in java

javamethodscharhex

提问by Cooper

how to convert a char from alphabetical character to hexadecimal number in java a if any one have any built-in method in java that does the job or if you have your own method ,could you please help

如果有人在java中有任何内置方法可以完成这项工作,或者如果您有自己的方法,如何在java中将字符从字母字符转换为十六进制数,请您帮忙

i forgot and also how to convert from hex to binary

我忘了以及如何从十六进制转换为二进制

回答by Peter Lawrey

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

回答by barti_ddu

You could use:

你可以使用:

Integer.toHexString((int) 'a');
Integer.toBinaryString((int) 'b');

Update: hex -> binary conversion:

更新:十六进制 -> 二进制转换:

Integer.toBinaryString(Integer.parseInt("fa", 16))