将包含二进制的整数列表转换为十六进制 [英] Converting integer list containing binary to hex
问题描述
我有一个包含一些二进制值的整数列表
List< Integer> binary = new ArrayList();
for(int i = 0; i <8; i ++)
{
bin [i] = pText [i] ^ ivKey [i]; //一些二进制值计算
binary.add(bin [i]); //二进制列表在这里得到更新
}
for(int i = 0; i< myCipher2.size(); i ++)
{
System.out .PRINT(binary.get(I)); //打印输出中显示的附加二进制文件
}
打印时的列表给出以下内容:
<00> 000001010100001110011101
每个8位指的是一个十六进制,所以我想在转换时的输出应该给我以下的内容:
05 43 9d
如何将列表拆分为8位并将这8位转换为十六进制?
ArrayList
添加一个类型: 列表<整数> binary = new ArrayList< Integer>();
由于它与您的问题没有直接关系,您可以阅读为什么您应该在此处
现在,既然你的列表包含单独的二进制数字,你可以做的是使用 StringBuilder
来构建8位二进制字符串。您还可以使用 StringBuilder
构建转换后的十六进制值。每次构建这个8位字符串时,都会将它转换为十六进制(实际上首先是十进制,然后是十六进制),然后将它附加到 StringBuilder
,然后生成十六进制字符串。
以下是代码:
// // 。你的程序
StringBuilder hexString = new StringBuilder();
StringBuilder eightBits = new StringBuilder(); (int i = 0; i for(int j = i; j<(i + 8)& j < binary.size(); j ++){//读取接下来的8位或其余的位是
eightBits.append(binary.get(j)); // build 8 bit value
}
int decimal = Integer.parseInt(eightBits.toString(),2); //获得十进制值
hexString.append(Integer.toHexString(decimal)+); //将十六进制值添加到十六进制字符串和一个可读性空间
eightBits.setLength(0); //重置,这样我们可以追加新的8位
System.out.println(hexString.toString()); //打印5 43 9d作为您给出的值
注意:这个程序是你程序的延续
I have a integer list which contains some binary values
List<Integer> binary = new ArrayList();
for (int i=0; i<8; i++)
{
bin[i] = pText[i] ^ ivKey[i]; //some binary value is calculated
binary.add(bin[i]); //the binary list gets updated here
}
for (int i=0; i<myCipher2.size(); i++)
{
System.out.print(binary.get(i)); //this prints the appended binary as shown in the output
}
The list when printed gives the following:
000001010100001110011101
Each 8 bits refers to a hex so the output I want when converted should give me the following
05 43 9d
How can I break the list into 8 bits and convert those 8 bits into hex?
First things first, you should not declare generic objects with raw types, add a type to your ArrayList
:
List<Integer> binary = new ArrayList<Integer>();
Since it isn't directly related to your question, you can read why you should do this here
Now, so since your list holds individual binary digits, what you can do is use a StringBuilder
to build 8-bit binary strings from it. You can also use a StringBuilder
to build the converted hex value. Each time you build this 8-bit string, you convert it to it to hex (actually first to decimal and then to hex) and append it to the StringBuilder
then builds the hex string.
Here is the code:
// ... your program
StringBuilder hexString = new StringBuilder();
StringBuilder eightBits = new StringBuilder();
for(int i = 0; i < binary.size(); i += 8) {
for(int j = i; j < (i + 8) && j < binary.size(); j++) { // read next 8 bits or whatever bits are remaining
eightBits.append(binary.get(j)); // build 8 bit value
}
int decimal = Integer.parseInt(eightBits.toString(), 2); // get decimal value
hexString.append(Integer.toHexString(decimal) + " "); // add hex value to the hex string and a space for readability
eightBits.setLength(0); // reset so we can append the new 8 bits
}
System.out.println(hexString.toString()); // prints "5 43 9d" for the value you gave
Note: this program is a continuation of your program
这篇关于将包含二进制的整数列表转换为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!