[英]convert string to hexadecimal string
i have a string of int value like "131008130225002" i need to convert it to hexadecimal string. 我有一个字符串整数值,例如“ 131008130225002”,我需要将其转换为十六进制字符串。 I tried various ways, 我尝试了各种方法
i need in hexadecimal format using ABC upto 12 places. 我需要使用ABC的十六进制格式最多12个位置。
I tried Integer.tohex, but it is out of range of integer 我尝试了Integer.tohex,但超出整数范围
My friend is doing same job in ios using unsigned long long as datatype and 0x%02llx regular expression to convert nsstring 我的朋友在使用unsigned long的 ios中做相同的工作, 只要数据类型和0x%02llx正则表达式可以转换nsstring
code is: 代码是:
String x="131008130225002";
System.out.println(x);
// System.out.println(Integer.parseInt(x));
System.out.println(Double.parseDouble(x));
System.out.println(Double.toHexString(Double.parseDouble(x)));
String a1= toHex(x);
System.out.println(a1);
toHex function: toHex函数:
static String toHex(String arg) {
try {
return String.format("%12x", new BigInteger(1, arg.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
String x = "131008130225002";
System.out.println(new BigInteger(x).toString(16));
The output is 7726b510936a. 输出为7726b510936a。
It will fit in a long
, so you can use Long.toHexString
. 它将long
,因此您可以使用Long.toHexString
。
System.out.println(Long.toHexString(Long.parseLong("131008130225002")));
For a more generic solution, BigInteger
also has a toString
function which takes in a radix (16 being hex of course). 对于更通用的解决方案, BigInteger
还具有一个toString
函数 ,该函数接受一个基数(当然16是十六进制)。
System.out.println(new BigInteger("131008130225002").toString(16));
Both of the above print out 7726b510936a
. 以上两个都打印出7726b510936a
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.