Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi all!

As beginner programmer Im not familiar with .NET methods to convert types to another so I kindly ask help with following problem.

I have empedded device which recieves messages as 8 bytes long byte array. One of its bytes represents output states. I have made .NET class and UI to control those states. Only problem is that cant cast my bool array into Byte..

Here is an example:

bool[] StatesArray = { false, true, true, false, false, false, false , false}; // States comes from MyClass properties changed by Ui component (eg. CheckBox)

//target device will read states as binary form eg. above state array: 01100000

byte[] Message = new byte[8]; //Byte array which will be The Message to be send (one index includes our states as Byte)

byte States; // byte which represents states 

//States = ???? // Problem! Convert StatesArray to one byte

//Message[4] = States; //Add our states into our message

//Send the message etc..


Hope you got idea :)

Cheers!
Posted

For instance:
C#
byte encodebool(bool[] arr)
{
  byte val = 0;
  foreach (bool b in arr)
  {
    val <<= 1;
    if (b) val |= 1;
  }
  return val;
 }
 
Share this answer
 
Comments
paleGegg0 22-Jun-11 5:12am    
Thanks so much, helped a lot :)!
Rather than using an array of bools, have you considered an enum:
public enum StatusByte : byte
    {
    Ready = 0,
    Abort = 1,
    TxReady = 2,
    RxReady = 3,
    TxError = 4,
    RxError = 5,
    }
private void button1_Click(object sender, EventArgs e)
    {
    StatusByte sb = ReadStatus();
    if ((int) (sb & StatusByte.Ready) == 1)
        {
        }
    }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jun-11 14:05pm    
Correct, byte[] makes little to no sense, my 5.
Right approach it in the answer by Prerak also.
--SA
bool[] StatesArray = { false, true, true, false, false, false, false , false}; 
BitArray bits = new BitArray(StatesArray);
byte[] Bytes = new byte[1];
bits.CopyTo(Bytes, 0);
//Bytes[0] is your answer
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jun-11 14:02pm    
Finally I see a correct solution! It should be bit array, not byte array, how it can be not clear? My 5.
--SA
Use Array.ConvertAll(Of TInput, TOutput)[^] method to acquire Message.

Then use this to join the bytes
C#
for(int i = 0;i<Message.Length-1;i++)states|=Message[i]<<i;
 
Share this answer
 
v7
Comments
Sergey Alexandrovich Kryukov 22-Jun-11 14:06pm    
This is because OP question is wrong of course. byte[] makes little to no sense. Right approach it in the answer by Prerak this time.
--SA
I think these line of code will help you.

C#
bool[] a = new bool[] { true, false, true, true, false, true };
byte[] b = (from x in a select x ? (byte)0x1 : (byte)0x0).ToArray();
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Jun-11 14:04pm    
This is because OP question is wrong of course. byte[] makes little to no sense. Right approach it in the answer by Prerak this time.
--SA
You could use LINQ

byte[] array = (from y in a select y ? (byte)0x1 : (byte)0x0).ToArray();
 
Share this answer
 
v2
Comments
CPallini 22-Jun-11 5:12am    
I suppose the expected output is one byte not a byte array.
Sergey Alexandrovich Kryukov 22-Jun-11 14:03pm    
This is because OP question is wrong of course. byte[] makes little to no sense. Right approach it in the answer by Prerak this time.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900