Converting a boolean array into a hexadecimal number(将布尔数组转换为十六进制数)
问题描述
有没有一种简单的方法可以将布尔值数组转换为 8 位十六进制等效值?例如,如果我有
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 位十六进制"是指字符串表示,则可以使用 BitConverter 类:
If by "8-bit hexadecimal" you mean the string representation, you can use the BitConverter class for that:
string hex = BitConverter.ToString(data);
这篇关于将布尔数组转换为十六进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将布尔数组转换为十六进制数
基础教程推荐
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
