简体   繁体   English

十六进制字符串到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.. 我遇到了如何将十六进制字符串转换为无符号字节数组或整数数组的问题。

Please look in this example. 请看这个例子。

String s= "1f5621ff8963545a12152250"; //this is my hex string

i want to convert this string to an int array 我想将此字符串转换为int数组

int buff[]={0x1f,0x56,0x21,0xff,0x89,0x63,0x54,0x5a,0x12,0x15,0x22,0x50};

how could i do that... 我怎么能这样做......

thanks for help.. 感谢帮助..

一个简单的方法:通过将String的长度除以2来计算int数组的长度。循环遍历字符串,调用substring()以创建双字符字符串“1f”,“56”等。传递每个字符串to Integer.parseInt(string, 16) (第二个参数表示参数是十六进制字符串。)在计算数据时将结果存储在数组中。

Iterate through the string and every two elements of the string create the integer like this: 遍历字符串,字符串的每两个元素都会创建如下的整数:

0Xmm, where mm are the two elements you've just scanned. 0Xmm,其中mm是您刚扫描的两个元素。

Then you increase the index of your array and keep iterating through the string and adding to the current "top" place of the array. 然后增加数组的索引并继续遍历字符串并添加到数组的当前“顶部”位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM