VB.NET转换帮助:将布尔数组转换为整数并将整数转换为布尔数组 [英] VB.NET Conversion Help: Bool Array to Integer and Integer to Bool Array
本文介绍了VB.NET转换帮助:将布尔数组转换为整数并将整数转换为布尔数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我想知道如何从一个命令行将32位布尔数组转换为整数值,而不是编写For循环.
Hello everyone,
I would like to know how to convert an 32 bit Boolean array to an integer value from one command line instead of writing a For Loop.
dim myBOOL(32) as Boolean
dim myINT as Integer
myBOOL(0) = True
myBOOL(1) = True
myBOOL(5) = True
myINT = xxx(myBOOL)
INT为35
-------------------------------------------------- ------
我也想以其他方式进行转换.
将整数转换为布尔数组.像...
where INT will be 35
--------------------------------------------------------
Also I would like to do the conversion other way round.
Convert an integer to a Boolean array. Like ...
dim myBOOL(32) as Boolean
dim myINT as Integer = 110
myBOOL = YYY(myINT)
其中myBOOL = 1101110
如果可以的话,请告诉我.
感谢
where myBOOL = 1101110
if this is possible to do please let me know.
Thanks
推荐答案
AFAIK,没有内置的方法可以做到没有循环:BitConverter类没有配备bool< =>转换.
因此,您将必须编写一些函数:
AFAIK, there is no built in way to do that without a loop: the BitConverter class is not equipped with bool <=> conversion.
So, you will have to write a couple of functions:
Private Shared Function ToBits(myInt As UInteger) As Boolean()
Dim result As Boolean() = New Boolean(31) {}
For i As Integer = 31 To 0 Step -1
result(i) = (myInt And &H80000000UI) <> 0
myInt <<= 1
Next
Return result
End Function
Private Shared Function FromBits(myBits As Boolean()) As UInteger
Dim result As UInteger = 0
Dim bit As UInteger = &H80000000UI
For i As Integer = 31 To 0 Step -1
If myBits(i) Then
result = result Or bit
End If
bit >>= 1
Next
Return result
End Function
使用它们非常明显:
To use them is pretty obvious:
Dim myBits As Boolean() = New Boolean(31) {}
Dim myInt As UInteger = &H12345678
myBits = ToBits(myInt)
myInt = FromBits(myBits)
这篇关于VB.NET转换帮助:将布尔数组转换为整数并将整数转换为布尔数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文