字符数组中的位
我需要将位转换为字符数组或字符串,帮助找到存储位的最佳方法,如果我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 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.
第一步是将位数组转换为字节,一旦有了字节数组,您将需要选择正确的编码并转换为字符数组的字符串:
显然,您需要知道这些位的编码为了能够转换为字符。如果您不知道编码,您应该重新考虑您要做什么。
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:
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.