简体   繁体   中英

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?

Why not simply do this?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM