转换一个布尔数组到一个十六进制数 [英] Converting a boolean array into a hexadecimal number

查看:202
本文介绍了转换一个布尔数组到一个十六进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个简单的方法来布尔值数组转换成8位十六进制equivlents?举例来说,如果我有

Is there an easy way to convert an array of boolean values into 8-bit hexadecimal equivlents? For example, if I have

 bool[] BoolArray = new bool[] { true,false,true,true,false,false,false,true };

如果真值= 1,假值= 0,那么我想一个方法或函数,将上面的数组转换为0xB1(10110001)。

If true values=1 and false values=0 then I'd like a method or function that would convert the above array to 0xB1 (10110001).

是否存在这样的函数或方法来做到这一点?我使用C#,顺便说一句。

Does there exist such a function or method to do this? I am using C#, by the way.

推荐答案

是的,你可以使用 BitArray 类。像这样的东西应该这样做:

Yes, you can use the BitArray class. Something like this should do it:

BitArray arr = new BitArray(BoolArray);
byte[] data = new byte[1];
arr.CopyTo(data, 0);

如果按8位十六进制你的意思是字符串重新presentation,您可以使用 BitConverter 类是:

If by "8-bit hexadecimal" you mean the string representation, you can use the BitConverter class for that:

string hex = BitConverter.ToString(data);

这篇关于转换一个布尔数组到一个十六进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆