BitArray 上的操作

发布于 2024-12-15 18:23:48 字数 288 浏览 5 评论 0原文

可能的重复:
如何将 BitArray 转换为单个 int?

我该如何将整数读取到 BitArray(6)(假设可以包含它)以及如何将 BitArray(6) 转换为无符号/有符号整数。

Possible Duplicate:
How i can convert BitArray to single int?

How can I read an integer to a BitArray(6) (assuming it can be contained) and how to convert a BitArray(6) to unsigned/signed integer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卷耳 2024-12-22 18:23:48
byte[] bytes = { 0, 0, 0, 25 };

// If the system architecture is little-endian (that is, little end first),
// reverse the byte array.
if (BitConverter.IsLittleEndian)
    Array.Reverse(bytes);

int i = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("int: {0}", i);
// Output: int: 25
byte[] bytes = { 0, 0, 0, 25 };

// If the system architecture is little-endian (that is, little end first),
// reverse the byte array.
if (BitConverter.IsLittleEndian)
    Array.Reverse(bytes);

int i = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("int: {0}", i);
// Output: int: 25
强者自强 2024-12-22 18:23:48
BitArray FromInt32(Int32 a)
{
    byte[] bytes = BitConverter.GetBytes(a);
    return new BitArray(bytes);
}

对于反向操作请参阅此问题,如前所述。

BitArray FromInt32(Int32 a)
{
    byte[] bytes = BitConverter.GetBytes(a);
    return new BitArray(bytes);
}

For reverse operaton see this question as mentioned before.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文