在 VB.NET 中将布尔值转换为整数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/745292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert Boolean to Integer in VB.NET
提问by Prankster
Take the following code:
取以下代码:
Sub Main()
Dim i As Integer
Dim b As Boolean
i = 1
b = i
i = b
Console.WriteLine(i)
i = Convert.ToInt32(b)
Console.WriteLine(i)
End Sub
This prints the following:
这将打印以下内容:
-1
1
Why is this?
为什么是这样?
(Just a joke :) You can get 0 too...
(只是个玩笑:)你也可以得到 0 ......
Int32.TryParse("True", i)
Console.WriteLine(i)
回答by JaredPar
What you're seeing is a bit of legacy code showing its head.
您所看到的是一些遗留代码显示其头部。
At the heart of the matter is the VT_BOOL type. Visual Basic 6.0 used the VT_BOOL type (AKA VARIANT_BOOL) for its boolean values. True for a VARIANT_BOOL is represented with the value VARIANT_TRUE which has the integer value -1. During the conversion to .NETit was decided that when using Visual Basic conversion routines to convert a boolean value to an Integer value, Visual Basic 6.0 semantics would be maintained on the return value; it would be -1.
问题的核心是 VT_BOOL 类型。Visual Basic 6.0 使用 VT_BOOL 类型(AKA VARIANT_BOOL)作为其布尔值。VARIANT_BOOL 的 True 用值 VARIANT_TRUE 表示,该值具有整数值 -1。在转换为.NET期间,决定使用 Visual Basic 转换例程将布尔值转换为整数值时,返回值将保持 Visual Basic 6.0 语义;它将是-1。
The first implicit conversion occurs with the b = i line. Under the hood this does an implicit conversion from integer to boolean. Any non-zero value is deemed to be true, therefore the resulting value is true.
第一个隐式转换发生在 b = i 行中。在幕后,这进行了从整数到布尔值的隐式转换。任何非零值都被视为真值,因此结果值为真。
However, the following line of code is doing an implicit conversion to an integer type.
但是,以下代码行将隐式转换为整数类型。
i = b
Under the hood this uses one of the Visual Basic conversion routines (CTypeor CInt) to convert the value to an integer. As such Visual Basic semantics are in play and the value returned is -1.
在幕后,它使用 Visual Basic 转换例程之一(CType或CInt)将值转换为整数。因此,Visual Basic 语义在起作用,返回的值为 -1。
The next interesting line is the Convert.ToInt32()
line. This is using a .NET conversion routine which does not use Visual Basic semantics. Instead, it returns the underlying BCLrepresentation for a true boolean value which is 1.
下一个有趣的线是Convert.ToInt32()
线。这是使用不使用 Visual Basic 语义的 .NET 转换例程。相反,它返回真实布尔值 1的底层BCL表示。
回答by Powerlord
Some languages consider boolean true to be -1 rather than 1. I'd have to do research to see why, as I don't recall.
某些语言认为 boolean true 为 -1 而不是 1。我必须进行研究以了解原因,因为我不记得了。
In VB6, the constant True
has the value -1
.
在 VB6 中,常量True
的值为-1
。
However, Convert.ToInt32(Boolean)
is documentedas returning "The number 1 if value is true; otherwise, 0." That way, it's the same no matter which framework language you're using.
但是,Convert.ToInt32(Boolean)
被记载为返回“数字1值是否为真;否则,0”。这样,无论您使用哪种框架语言,它都是一样的。
Edit: See the question boolean true -- positive 1 or negative 1
回答by RolandTumble
As to why -1 is used for True, I believe that it's because it is literally (NOT 0).
至于为什么 -1 用于 True,我相信这是因为它是字面意思(不是 0)。
Start with zero, flip all the bits and then read it as two's complement--comes out negative one.
从零开始,翻转所有位,然后将其作为二进制补码读取——结果为负一。
So since anything that is not False is True, and False is 0, the (NOT False) is represented by -1.
因此,由于任何不是 False 的东西都是 True,而 False 是 0,所以 (NOT False) 由 -1 表示。
This may be just coincidence, though....
不过,这可能只是巧合……
回答by Arnold Spence
From MSDN Visual Basic documentation:
来自 MSDN Visual Basic 文档:
When Visual Basic converts numeric data type values to Boolean, 0 becomes False and all other values become True. When Visual Basic converts Boolean values to numeric types, False becomes 0 and True becomes -1.
当 Visual Basic 将数值数据类型值转换为布尔值时,0 变为 False,所有其他值变为 True。当 Visual Basic 将布尔值转换为数字类型时,False 变为 0,True 变为 -1。
And for Convert.ToInt32(value):
retuns the number 1 if value is true; otherwise, 0.
如果值为真,则返回数字 1;否则为 0。
So for your code:
所以对于你的代码:
i = 1
b = i // b becomes true
i = b // true = -1
Console.WriteLine(i) // i is -1
i = Convert.ToInt32(b) // Convert.ToInt32(true) = 1
Console.WriteLine(i) // i is 1
回答by Goldmoon
"True" is an negation from the 0 value of a numeric data type!
“True”是对数值数据类型的 0 值的否定!
Not(0) for non-signed types returns 1.
Not(0) 对于非签名类型返回 1。
Not(0) for signed types returns -1.
Not(0) 对于有符号类型返回 -1。
I don't know your code, maybe your code performs a internal data conversion the second time.
我不知道你的代码,也许你的代码第二次执行了内部数据转换。
回答by D.Alexander
This is a dodgy answer but:
这是一个狡猾的答案,但是:
Dim b As Boolean
b = False
Dim i As Integer
i = IIf(b, 1, 0)
回答by Matt Grande
回答by Goldmoon
All numeric data types can used as Boolean! The result is dependent on the used data type.
所有数字数据类型都可以用作布尔值!结果取决于使用的数据类型。
Examples:
例子:
Dim i As Byte ' Byte is non-signed!
Dim b As Boolean = True
i = b ' Set first (lowest) bit of i (non-signed byte)
' i is now binary 0000 0001 = 1!
Dim i As SByte ' SByte is signed!
Dim b As Boolean = True
i = b ' Set all bits of i (signed byte)
' i is now FF (binary 1111 1111 = -1 !
Integer is signed, True to Integer -> -1.
整数是有符号的,真到整数 -> -1。
UInteger is non-signed, True to UInt -> 1.
UInteger 是无符号的,True 到 UInt -> 1。
And so on...
等等...
A false value clears the highest bit in signed numerics, and lowest in non-signed numerics.
假值清除有符号数字中的最高位,以及无符号数字中的最低位。
Therefore False is 0 in all numeric data types.
因此 False 在所有数值数据类型中都是 0。