字符数组中的位

发布于 2024-09-26 08:42:21 字数 71 浏览 0 评论 0原文

我需要将位转换为字符数组或字符串,帮助找到存储位的最佳方法,如果我有 18 位,我将制作 2 个字符和 2 个位,我应该做什么?

I need to transfrom bits into char array or string, help to find best way to store bits and what shoud I do if for example I have 18 bits I will make 2 char and 2 bits?

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

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

发布评论

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

评论(2

甩你一脸翔 2024-10-03 08:42:21

在 C# 中存储位的最佳方法是在 BitArray 类中(如果您只需要将它们作为位)。如果您需要 18 位的整数值,那么您必须将它们转换为 int 或 double 等。

The best way to store bits in C# is in the BitArray class, if you just need them as bits. If you need the integer value of the 18 bits, then you have to convert them to int or double or whatever.

反目相谮 2024-10-03 08:42:21

第一步是将位数组转换为字节,一旦有了字节数组,您将需要选择正确的编码并转换为字符数组的字符串:

BitArray bitArray = new BitArray(new[] { true, false, true, false, });
byte[] bytes = new byte[bitArray.Length];
bitArray.CopyTo(bytes, 0);
char[] result = Encoding.UTF8.GetString(bytes).ToCharArray();

显然,您需要知道这些位的编码为了能够转换为字符。如果您不知道编码,您应该重新考虑您要做什么。

First step would be to convert your bit array into bytes and once you have an array of bytes you will need to choose a proper encoding and convert to a string which is an array of chars:

BitArray bitArray = new BitArray(new[] { true, false, true, false, });
byte[] bytes = new byte[bitArray.Length];
bitArray.CopyTo(bytes, 0);
char[] result = Encoding.UTF8.GetString(bytes).ToCharArray();

Obviously you need to know the encoding of those bits in order to be able to convert to characters. If you don't know the encoding you should reconsider what you are trying to do.

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