如何自动转换十六进制代码在Java中使用它作为byte []? [英] How to autoconvert hexcode to use it as byte[] in Java?
本文介绍了如何自动转换十六进制代码在Java中使用它作为byte []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里有很多的十六进制代码,我想让它们进入Java而不会将0x附加到每个实体。像:
0102FFAB我必须做以下事情:
byte [] test = {0x01,0x02,0xFF,0xAB};
我有很多很长的hexcode。有什么办法可以自动做到这一点?
解决方案
您可以尝试将十六进制代码放入一个字符串中,然后迭代字符串,与此类似:
String input =0102FFAB;
byte [] bytes = new byte [input.length()/ 2];
for(int i = 0; i< input.length(); i + = 2)
{
bytes [i / 2] = Integer.decode(0x + input.substring(i,i + 2)).byteValue();
请注意,这需要甚至长度字符串,这是一个相当快速和肮脏的解决方案。不过,它仍应该让你开始。
I have many hexcodes here and I want to get them into Java without appending 0x to every entity. Like:
0102FFAB and I have to do the following:
byte[] test = {0x01, 0x02, 0xFF, 0xAB};
And I have many hexcodes which are pretty long. Is there any way to make this automatically?
解决方案
You could try and put the hex codes into a string and then iterate over the string, similar to this:
String input = "0102FFAB";
byte[] bytes = new byte[input.length() / 2];
for( int i = 0; i < input.length(); i+=2)
{
bytes[i/2] = Integer.decode( "0x" + input.substring( i, i + 2 ) ).byteValue();
}
Note that this requires even length strings and it is quite a quick and dirty solution. However, it should still get you started.
这篇关于如何自动转换十六进制代码在Java中使用它作为byte []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文