函数将十六进制字符串转换为 BitArray C#
我创建了以下函数,它将按要求执行(将十六进制字符串转换为 BitArray)。我不确定该函数的效率,但我现在的主要问题是 Convert.ToInt64 函数是特定于字节序的。当将其移植到备用芯片组时,我们将得到不同的结果(或例外)。那么有人能想到另一种方法来进行这种转换吗???
public BitArray convertHexToBitArray(string hexData)
{
string binary_values = "";
BitArray binary_array;
if (hexData.Length <= "FFFFFFFFFFFFFFFF".Length) // Max Int64
{
binary_values = Convert.ToString(Convert.ToInt64(hexData, 16), 2);
binary_array = new BitArray(binary_values.Length);
for (int i = 0; i < binary_array.Length; i++)
{
if (binary_values[i] == '0')
{
binary_array[i] = false;
}
else
{
binary_array[i] = true;
}
}
}
}
我删除了大部分错误/异常处理以保持其大小,所以请原谅。
I created the following function which will do as requested (convert HEX string to BitArray). I am not sure about the efficiency of the function, but my main problem now is that the Convert.ToInt64 function is endian specific. When this is ported over to alternate chipsets we will get different results (or exceptions). So can anyone think of an alternate way to do this conversion???
public BitArray convertHexToBitArray(string hexData)
{
string binary_values = "";
BitArray binary_array;
if (hexData.Length <= "FFFFFFFFFFFFFFFF".Length) // Max Int64
{
binary_values = Convert.ToString(Convert.ToInt64(hexData, 16), 2);
binary_array = new BitArray(binary_values.Length);
for (int i = 0; i < binary_array.Length; i++)
{
if (binary_values[i] == '0')
{
binary_array[i] = false;
}
else
{
binary_array[i] = true;
}
}
}
}
I removed most of the error / exception handling to keep this to size so plz forgive that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是一个简单的答案,应该适用于任何长度的字符串:
here is a simple answer, should work with a string of any length:
试试这个:
Try this:
我还没有测试过这个(认为它是伪代码),但它会很快:
还有守卫:
I haven't tested this (consider it pseduo code), but it would be fast:
And the guards: