如何从十六进制字符串中获取位范围并将其转换为整数?
How to get a bit range from a hexadecimal string and convert it to an integer?
我问这个问题,因为找不到针对这个特殊情况的解决方案。
我需要从十六进制字符串和给定的位范围中提取值(作为 int 或字节)。例如:
hexString = "0x34840A00"
位(来自计算):00110100 10000100 00001010 00000000
现在我需要从 8 到 10 的位:010
现在转换为 int/byte 值。有什么帮助吗?我正在努力理解这里的位操作。
您可以先使用 Convert.FromHexString(hexString)
从十六进制字符串中获取字节。然后您可以使用不安全指针或 BitConverter.ToInt32()
将所述字节转换为 32 位整数,然后您可以对其应用位移和其他按位操作来提取所需的位。例如:
string hexString = "0x34840A00";
byte[] bytes = Convert.FromHexString(hexString);
int myInt = BitConverter.ToInt32(bytes, 0);
// myInt looks like this 00110100 10000100 00001010 00000000
// now shift by 8 bits to the right
myInt >>= 8;
// now it looks like this 00000000 00110100 10000100 00001010
// to get the value of the last 3 bits we need to set the first 29 bits to 0
// so we AND them together with 29 0's followed by 3 1's:
myInt &= 0x00000007;
// which results in the following bits:
// 00000000 00000000 00000000 00000010
// leaving you with an integer of the value 010 in binary or 2 in decimal
位移位和与运算始终遵循相同的规则:
- 如果您想要从索引
n
开始的位(从 right-most/least 有效位开始计算)然后右移 n
位(在您的示例中 n=8
(你不关心最右边的 8 位)。
- 设
m
为您希望从位置 n
开始保留的位数(在您的示例中,您希望保留 3 位 010
)。 AND 结果连同整数 2^m - 1
。在您的示例 m=3
中,这将是 2^3-1 = 8-1 = 7
。因此我们做 myInt &= 0x00000007;
或简而言之 myInt &= 0x7;
.
编辑:针对旧的 .NET 版本和简化
string hexString = "0x34840A00";
int myInt = Convert.ToInt32(hexString, 16);
// myInt looks like this 00110100 10000100 00001010 00000000
// now shift by 8 bits to the right
myInt >>= 8;
// now it looks like this 00000000 00110100 10000100 00001010
// to get the value of the last 3 bits we need to set the first 29 bits to 0
// so we AND them together with 29 0's followed by 3 1's:
myInt &= 0x00000007;
// which results in the following bits:
// 00000000 00000000 00000000 00000010
// leaving you with an integer of the value 010 in binary or 2 in decimal
因此,可重用(并且相当快)的方法将是这样的方法:
private static int GetBitsFromHexString(string hexString, int startIndexFromRight, int bitsToKeep)
{
int result = Convert.ToInt32(hexString, 16);
// shift by startIndexFromRight bits to the right
result >>= startIndexFromRight;
// AND the bits together with 32 - bitsToKeep 0's followed by bitsToKeep 1's:
int bitmask = (1 << bitsToKeep) - 1; // is the same as 2^bitsToKeep - 1.
// apply bitmask
result &= bitmask;
return result;
}
在你的例子中你会这样称呼:
int result = GetBitsFromHexString("0x34840A00", 8, 3);
// result is 2 or 010 in binary
我问这个问题,因为找不到针对这个特殊情况的解决方案。
我需要从十六进制字符串和给定的位范围中提取值(作为 int 或字节)。例如:
hexString = "0x34840A00"
位(来自计算):00110100 10000100 00001010 00000000
现在我需要从 8 到 10 的位:010
现在转换为 int/byte 值。有什么帮助吗?我正在努力理解这里的位操作。
您可以先使用 Convert.FromHexString(hexString)
从十六进制字符串中获取字节。然后您可以使用不安全指针或 BitConverter.ToInt32()
将所述字节转换为 32 位整数,然后您可以对其应用位移和其他按位操作来提取所需的位。例如:
string hexString = "0x34840A00";
byte[] bytes = Convert.FromHexString(hexString);
int myInt = BitConverter.ToInt32(bytes, 0);
// myInt looks like this 00110100 10000100 00001010 00000000
// now shift by 8 bits to the right
myInt >>= 8;
// now it looks like this 00000000 00110100 10000100 00001010
// to get the value of the last 3 bits we need to set the first 29 bits to 0
// so we AND them together with 29 0's followed by 3 1's:
myInt &= 0x00000007;
// which results in the following bits:
// 00000000 00000000 00000000 00000010
// leaving you with an integer of the value 010 in binary or 2 in decimal
位移位和与运算始终遵循相同的规则:
- 如果您想要从索引
n
开始的位(从 right-most/least 有效位开始计算)然后右移n
位(在您的示例中n=8
(你不关心最右边的 8 位)。 - 设
m
为您希望从位置n
开始保留的位数(在您的示例中,您希望保留 3 位010
)。 AND 结果连同整数2^m - 1
。在您的示例m=3
中,这将是2^3-1 = 8-1 = 7
。因此我们做myInt &= 0x00000007;
或简而言之myInt &= 0x7;
.
编辑:针对旧的 .NET 版本和简化
string hexString = "0x34840A00";
int myInt = Convert.ToInt32(hexString, 16);
// myInt looks like this 00110100 10000100 00001010 00000000
// now shift by 8 bits to the right
myInt >>= 8;
// now it looks like this 00000000 00110100 10000100 00001010
// to get the value of the last 3 bits we need to set the first 29 bits to 0
// so we AND them together with 29 0's followed by 3 1's:
myInt &= 0x00000007;
// which results in the following bits:
// 00000000 00000000 00000000 00000010
// leaving you with an integer of the value 010 in binary or 2 in decimal
因此,可重用(并且相当快)的方法将是这样的方法:
private static int GetBitsFromHexString(string hexString, int startIndexFromRight, int bitsToKeep)
{
int result = Convert.ToInt32(hexString, 16);
// shift by startIndexFromRight bits to the right
result >>= startIndexFromRight;
// AND the bits together with 32 - bitsToKeep 0's followed by bitsToKeep 1's:
int bitmask = (1 << bitsToKeep) - 1; // is the same as 2^bitsToKeep - 1.
// apply bitmask
result &= bitmask;
return result;
}
在你的例子中你会这样称呼:
int result = GetBitsFromHexString("0x34840A00", 8, 3);
// result is 2 or 010 in binary