ValentinDP
ValentinDP

Reputation: 323

C# convert bool to byte[4]

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?

Upvotes: 0

Views: 395

Answers (1)

Bart van der Drift
Bart van der Drift

Reputation: 1336

Why not simply do this?

bool DROIT_PRIO = false;
byte[] TYPE_DROIT_ARRAY = new byte[] { (byte)(DROIT_PRIO ? 0x1 : 0x0), 0x0, 0x0, 0x0 };

Upvotes: 4

Related Questions