Discussion:
what is BOOL?
(too old to reply)
bill tie
2009-12-12 21:31:01 UTC
Permalink
Raw Message
How do I convert a Win32 BOOL value to a C# bool value? To be precise, my C#
program has a 4-byte array. It knows the 4-byte array represents BOOL. How
should the program get a C# bool value from the byte arry?

Thank you.
Jeroen Mostert
2009-12-13 09:51:18 UTC
Permalink
Raw Message
Post by bill tie
How do I convert a Win32 BOOL value to a C# bool value? To be precise, my C#
program has a 4-byte array. It knows the 4-byte array represents BOOL. How
should the program get a C# bool value from the byte arry?
bool boolValue = BitConverter.ToInt32(bytes, 0) != 0;
--
J.
bill tie
2009-12-13 13:13:01 UTC
Permalink
Raw Message
Post by Jeroen Mostert
bool boolValue = BitConverter.ToInt32(bytes, 0) != 0;
I did something similar. Jeroen, can you confirm that BOOL
- is of the type int,
- is 32-bit long,
- means logical True if the integer value is non-zero, thus it may be
positive or negative,
- means logical False if the integer value is zero.
Jeroen Mostert
2009-12-13 21:05:39 UTC
Permalink
Raw Message
Post by bill tie
Post by Jeroen Mostert
bool boolValue = BitConverter.ToInt32(bytes, 0) != 0;
I did something similar. Jeroen, can you confirm that BOOL
- is of the type int,
- is 32-bit long,
- means logical True if the integer value is non-zero, thus it may be
positive or negative,
- means logical False if the integer value is zero.
Wellll, if you want to get pedantic: not exactly. BOOL is a 32-bit integer
(regardless of platform bitness) that, according to the rules of Win32,
*must* be either TRUE (defined as 1) or FALSE (defined as 0). So it's
stricter than a C BOOL. However, in practice the type is always used in
expressions like

BOOL x = ...;
if (x) { ... }

which obeys the rules of C boolean testing, of course, meaning that any
nonzero value is true and zero is false (C didn't get a separate boolean
type until C99, which is not widely used). So the conversion as given above
will work for BOOLs that obey the rules and BOOLs that don't obey the Win32
rules but will work in practice.

When generating BOOL values yourself, you should of course take care to play
nice and produce only 0 and 1.
--
J.
Jeroen Mostert
2009-12-13 21:10:01 UTC
Permalink
Raw Message
So it's stricter than a C BOOL.
This sentence should be taken metaphorically, as there's no such thing as a
"C BOOL", as I mention myself. :-) The C standard always speaks of "an
expression that compares unequal to 0" when it needs to describe what
ordinary mortals call a "boolean expression".
--
J.
Loading...