十六进制字符串为int数组转换 [英] Hex string to int array conversion
问题描述
我遇到了如何将十六进制字符串转换为无符号字节数组或整数数组的问题。
I am stuck with these problem that how to convert a hex string to unsigned byte array or an Integer array..
请看这个例子。
String s= "1f5621ff8963545a12152250"; //this is my hex string
我想将此字符串转换为一个int数组
i want to convert this string to an int array
int buff[]={0x1f,0x56,0x21,0xff,0x89,0x63,0x54,0x5a,0x12,0x15,0x22,0x50};
我怎么能这么做...
how could i do that...
感谢您的帮助。
推荐答案
一种简单的方法:计算 int <通过将
String
的长度除以2来获得code>数组。 >创建两个字符的字符串1f,56等。将每个字符串传递给 Integer.parseInt(string,16)
(第二个参数表示参数是一个十六进制字符串。)当您计算结果时,将结果存储在数组中。
One easy way: compute the length of your int
array by dividing the length of the String
by 2. Loop over the string, calling substring()
to create the two-character strings "1f", "56", etc. Pass each string to Integer.parseInt(string, 16)
(the second argument means that the argument is a hex string.) Store the results in the array as you compute them.
这篇关于十六进制字符串为int数组转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!