Kal*_*son 194
这是将其转换为十六进制的简短方法:
public String toHex(String arg) {
return String.format("%040x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
}
Run Code Online (Sandbox Code Playgroud)
小智 62
为确保十六进制长度始终为40个字符,BigInteger必须为正数:
public String toHex(String arg) {
return String.format("%x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
}
Run Code Online (Sandbox Code Playgroud)
Jos*_*ink 43
import org.apache.commons.codec.binary.Hex;
...
String hexString = Hex.encodeHexString(myString.getBytes(/* charset */));
Run Code Online (Sandbox Code Playgroud)
http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Hex.html
Ste*_*nne 18
您编码为十六进制的数字必须代表字符的某些编码,例如UTF-8.因此,首先将String转换为表示该编码中的字符串的byte [],然后将每个字节转换为十六进制.
public static String hexadecimal(String input, String charsetName) throws UnsupportedEncodingException {
if (input == null) throw new NullPointerException();
return asHex(input.getBytes(charsetName));
}
private static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();
public static String asHex(byte[] buf)
{
char[] chars = new char[2 * buf.length];
for (int i = 0; i < buf.length; ++i)
{
chars[2 * i] = HEX_CHARS[(buf[i] & 0xF0) >>> 4];
chars[2 * i + 1] = HEX_CHARS[buf[i] & 0x0F];
}
return new String(chars);
}
Run Code Online (Sandbox Code Playgroud)
S. *_*rin 16
Java 17引入了用于十六进制格式化的实用程序类:java.util.HexFormat
转换为十六进制:
public String toHex(String value) {
return HexFormat.of().formatHex(value.getBytes());
}
Run Code Online (Sandbox Code Playgroud)
从十六进制转换:
public String fromHex(String value) {
return new String(HexFormat.of().parseHex(value));
}
Run Code Online (Sandbox Code Playgroud)
有关 HexFormat 的更多信息请参见此处
文档:这里
Bul*_*aza 15
用途DatatypeConverter.printHexBinary():
public static String toHexadecimal(String text) throws UnsupportedEncodingException
{
byte[] myBytes = text.getBytes("UTF-8");
return DatatypeConverter.printHexBinary(myBytes);
}
Run Code Online (Sandbox Code Playgroud)
用法示例:
System.out.println(toHexadecimal("Hello StackOverflow"));
Run Code Online (Sandbox Code Playgroud)
打印:
48656C6C6F20537461636B4F766572666C6F77
Run Code Online (Sandbox Code Playgroud)
jor*_*deu 10
这是另一种解决方案
public static String toHexString(byte[] ba) {
StringBuilder str = new StringBuilder();
for(int i = 0; i < ba.length; i++)
str.append(String.format("%x", ba[i]));
return str.toString();
}
public static String fromHexString(String hex) {
StringBuilder str = new StringBuilder();
for (int i = 0; i < hex.length(); i+=2) {
str.append((char) Integer.parseInt(hex.substring(i, i + 2), 16));
}
return str.toString();
}
Run Code Online (Sandbox Code Playgroud)
所有基于String.getBytes()的答案都涉及根据Charset对字符串进行编码.您不一定得到构成字符串的2字节字符的十六进制值.如果您真正想要的是十六进制查看器,那么您需要直接访问这些字符.这是我在代码中用于调试Unicode问题的函数:
static String stringToHex(String string) {
StringBuilder buf = new StringBuilder(200);
for (char ch: string.toCharArray()) {
if (buf.length() > 0)
buf.append(' ');
buf.append(String.format("%04x", (int) ch));
}
return buf.toString();
}
Run Code Online (Sandbox Code Playgroud)
然后,stringToHex("testing123")将为您提供:
0074 0065 0073 0074 0069 006e 0067 0031 0032 0033
Run Code Online (Sandbox Code Playgroud)
我会建议像这样的东西,str输入字符串在哪里:
StringBuffer hex = new StringBuffer();
char[] raw = tokens[0].toCharArray();
for (int i=0;i<raw.length;i++) {
if (raw[i]<=0x000F) { hex.append("000"); }
else if(raw[i]<=0x00FF) { hex.append("00" ); }
else if(raw[i]<=0x0FFF) { hex.append("0" ); }
hex.append(Integer.toHexString(raw[i]).toUpperCase());
}
Run Code Online (Sandbox Code Playgroud)
byte[] bytes = string.getBytes(CHARSET); // you didn't say what charset you wanted
BigInteger bigInt = new BigInteger(bytes);
String hexString = bigInt.toString(16); // 16 is the radix
Run Code Online (Sandbox Code Playgroud)
你可以hexString在这一点上返回,但要注意前导空字符将被剥离,如果第一个字节小于16,结果将有一个奇数长度.如果你需要处理这些情况,你可以添加一些额外的代码用0填充:
StringBuilder sb = new StringBuilder();
while ((sb.length() + hexString.length()) < (2 * bytes.length)) {
sb.append("0");
}
sb.append(hexString);
return sb.toString();
Run Code Online (Sandbox Code Playgroud)
获取十六进制的Integer值
//hex like: 0xfff7931e to int
int hexInt = Long.decode(hexString).intValue();
Run Code Online (Sandbox Code Playgroud)
转换为十六进制代码的字母和十六进制代码的字母。
String letter = "a";
String code;
int decimal;
code = Integer.toHexString(letter.charAt(0));
decimal = Integer.parseInt(code, 16);
System.out.println("Hex code to " + letter + " = " + code);
System.out.println("Char to " + code + " = " + (char) decimal);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
390632 次 |
| 最近记录: |