0

I need to convert a 4-byte bool for a transmission to a server.

This is my value:

 bool DROIT_PRIO = false;

I already try like this:

var TYPE_DROIT_ARRAY = BitConverter.GetBytes(DROIT_PRIO);

Obtained result (in hexa): 00

Expected result :

False : 00-00-00-00

True : 01-00-00-00

How I can pass my value in bytes[] to obtain this result?

ValentinDP
  • 323
  • 5
  • 14
  • 2
    Use `BitConverter.GetBytes` and store the obtained value in a `byte[4]` array. – dymanoid May 24 '19 at 13:41
  • 1
    Use `Convert.ToInt32`. There are no 4-byte bools, the type only needs 1 bit. 4 or 8 bytes are used for a bool though because that's the word size in a 32 or 64-bit OS. – Panagiotis Kanavos May 24 '19 at 13:46
  • 1
    `bool` is *always* one byte in c#, and its low-order bit is used to represent its value. –  May 24 '19 at 13:46
  • How do you want true to be represented? – NineBerry May 24 '19 at 13:47
  • @Amy [not always](https://stackoverflow.com/questions/2308034/primitive-boolean-size-in-c-sharp) – Panagiotis Kanavos May 24 '19 at 13:48
  • [Convert.ToInt32](https://github.com/microsoft/referencesource/blob/master/mscorlib/system/convert.cs#L1033) simply checks the value and returns the predifiend [Boolean.True](https://github.com/microsoft/referencesource/blob/master/mscorlib/system/boolean.cs#L38) or [Boolean.False](https://github.com/microsoft/referencesource/blob/master/mscorlib/system/boolean.cs#L42) values: `return value? Boolean.True: Boolean.False;` – Panagiotis Kanavos May 24 '19 at 13:49
  • @Amy that's... not quite the case; `bool` doesn't really exist as an intrinsic - as locals, it is typically backed by int or native int, which means it is usually 4 or 8 bytes - although for fields, it gets messier – Marc Gravell May 24 '19 at 13:49
  • @MarcGravell Was quoting the documentation on `System.Boolean`. –  May 24 '19 at 13:51
  • @Amy the documentation there is ... let's just say "over-simplified"; and re the "low order bit" thing - even that gets tricky; in most cases, any non-zero value will be treated as true; in other cases, it is more picky and only `true` will be true. If you use `unsafe` code or IL to play with the underlying value of a `bool`, really really fun (read: not fun at all) things can happen! – Marc Gravell May 24 '19 at 13:54
  • @Amy if you try `Marshal.SizeOf(true)` you'll get 4, while `sizeof(bool)` returns 1. The size of *this* particular type isn't the same as the size it takes up in memory. A CPU register can only be 4 or 8 bytes, so without *packing* multiple bools in a sinle int, you'll always get at least 4 bytes per boolean – Panagiotis Kanavos May 24 '19 at 13:57
  • @Amy here's a fun illustration of this: https://gist.github.com/mgravell/c841f2a208eb8dcfb7a91f9ae6078a55 - it caught me out the first time I saw it, too! – Marc Gravell May 24 '19 at 13:58
  • @Amy the rest is Marc's and Jon's responsibility to sort out and document in an unambiguous way in the C# specification – Panagiotis Kanavos May 24 '19 at 13:58
  • @MarcGravell Interesting, I'll take some time later to unpack that gist after I've had more coffee. –  May 24 '19 at 13:59
  • @NineBerry I've edited my post – ValentinDP May 24 '19 at 14:08

1 Answers1

4

Why not simply do this?

bool DROIT_PRIO = false;
byte[] TYPE_DROIT_ARRAY = new byte[] { (byte)(DROIT_PRIO ? 0x1 : 0x0), 0x0, 0x0, 0x0 };
Bart van der Drift
  • 1,287
  • 12
  • 30