如何从左移二进制和解释十六进制数字节数组?
我的android应用程序正在接收从C#应用程序发送的数据字节数组。我需要解释这些字节。如何从左移二进制和解释十六进制数字节数组?
在C#应用程序中,表单中有16个复选框(Bit0到Bit15),代码显示这些复选框结果的处理。
ushort flag = (ushort)(
(Bit0.Checked ? (1 << 0) : (0)) +
(Bit1.Checked ? (1 << 1) : (0)) +
(Bit2.Checked ? (1 << 2) : (0)) +
(Bit3.Checked ? (1 << 3) : (0)) +
(Bit4.Checked ? (1 << 4) : (0)) +
(Bit5.Checked ? (1 << 5) : (0)) +
(Bit6.Checked ? (1 << 6) : (0)) +
(Bit7.Checked ? (1 << 7) : (0)) +
(Bit8.Checked ? (1 << 8) : (0)) +
(Bit9.Checked ? (1 << 9) : (0)) +
(Bit10.Checked ? (1 << 10) : (0)) +
(Bit11.Checked ? (1 << 11) : (0)) +
(Bit12.Checked ? (1 << 12) : (0)) +
(Bit13.Checked ? (1 << 13) : (0)) +
(Bit14.Checked ? (1 << 14) : (0)) +
(Bit15.Checked ? (1 << 15) : (0)));
flag
传递给下面描述的功能,然后将其发送到我的Android应用程序。
public static void setFlag(List<Byte> data, ushort flag)
{
for (int i = 0; i < 2; i++)
{
int t = flag >> (i * 8);
data.Add((byte)(t & 0x00FF));
}
}
在Android应用,该数据被接收到的为4个字节的数组,然后将其转换为十进制
public String bytesToAscii(byte[] data) {
String str = new String(data);
return str.trim();
}
// This returns the decimal
Integer.parseInt(bytesToAscii(flag), 16)
比方说,例如,当位13在C#申请被检查;安卓应用接收的4个字节的数组表示十六进制数:
flag[0] = 0x30;
flag[1] = 0x30;
flag[2] = 0x32;
flag[3] = 0x30;
它被转换为0020
,然后将其转换为十进制:
Integer.parseInt(bytesToAscii(flag), 16); // 32
我需要解析32
找出位13被选中。 Bit13只是32的一个例子。我需要确定选择了哪一个或多个Bit(0到15)。
要检查是否设置了一位,可以对该位执行按位与操作。然后检查结果是否等于0.如果不是,则该位被设置。
例如
00100110
00000010 // checks the second bit
-------- &
00000010 // result != 0, so the bit was set
一个char
是无符号的16位,所以你可以用它来存储结果。
0020
几乎是正确的,但字节相反(00 20
,应为20 00
为Bit13)。
byte[] flag = new byte[4];
flag[0] = 0x30;
flag[1] = 0x30;
flag[2] = 0x32;
flag[3] = 0x30;
// Bytes to char, using the 'oversized' short so the numbers won't be out of range
short b1 = Short.parseShort(new String(new byte[]{flag[0], flag[1]}), 16);
short b2 = Short.parseShort(new String(new byte[]{flag[2], flag[3]}), 16);
char i = (char) (b1 | (b2 << 8));
// Print contents as binary string
System.out.println(String.format("%16s", Integer.toBinaryString(i)).replace(' ', '0'));
// Output: 0010000000000000
// Check if 14'th bit is set (at index 13)
boolean isSet = ((i & (1 << 13)) != 0);
System.out.println(isSet); // true
您可以使用该方法检查每一位。只需将13
替换为您要检查的索引即可。
我使用的是char
在这里,因为这将打印更好一点。您可以使用short
,但每当您将其转换为int
(可能会隐式发生)时,如果设置了最高有效位,则会使用1
填充值,因为这是签署的类型。 char
是但是没有签名,所以它没有这种行为。
根据我的示例'0x30 0x30 0x32 0x30',唯一设置的位是Bit13。为什么在结果中有很多位是'1'? – Sithu
@Sithu这只是一个例子......但我终于想出了你的转换应该如何工作。查看更新的答案。 –
谢谢。这几乎奏效。但是,当我打开所有位时,该值为'FFFF',相当于0x46 0x46 0x46 0x46,它会抛出java.lang.NumberFormatException:值超出范围。值:“FFFF”基数:16在java.lang.Short.parseShort(未知源)'。当Bit15以外的所有位都关闭时,'FF7F'的错误同样出现。 – Sithu
为什么你收到的数据是4字节而不是2字节的数组? – user0815
你为什么乘以8?如果你有两个字节,那么data [0] jdweng
@JornVernee他加了2 Bytes(0和1) - 但我没有得到转换的整个过程... – user0815