Click here to Skip to main content
15,795,398 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
<pre>// I AM TRYING CONVERT A BOOLEAN ARRAY (WITH 8 VALUES) TO A BYTE
// I AM ALSO TRYING TO WRITE THE CONVERTED BYTE TO A FILE.
using System;
using System.IO;
using System.Collections;
namespace Applica
{
    static class Program
    {
        static void Main(string[] args)
        {
            byte val;
            bool tif = true;
            bool[] array = new bool[8];
            const int BufferSize = 1024;
            byte[] buffer = new byte[BufferSize];
            FileInfo ap = new FileInfo("tempii.txt");
            string filePath = ap.FullName;
            string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));
            using (Stream output = File.OpenWrite(destinationPath))
            {
                int bytes = 1;
                while (bytes > 0)
                {
                    for (int i = 0; i < 8; i++)
                    {
                        if (tif == true)
                        {
                            tif = false;
                        }
                        else if (tif == false)
                        {
                            tif = true;
                        }
                        array[i] = tif;
                        if (i == 7)
                        {
                            val = Convert.ToByte(array);//Unable to cast object of type 'System.Boolean[]' to type 'System.IConvertible'
                        }
                    }
                    output.Write(buffer, 0, bytes);// I WANT TO WRITE "VAL" TO OUTPUT FILE.
                }
            }
        }
    }
}






What I have tried:

I am having trouble with last two lines of the code. I am trying to convert a boolean array with 8 values to a byte. Then I am trying to write the converted byte to a file.
Posted
Updated 29-Nov-17 7:11am

1 solution

You can scrap a lot of that code just by using a BitArray to do the conversion:
C#
bool[] array = new bool[8];
array[1] = true;
array[3] = true;
array[6] = true;
array[7] = true;

BitArray bytes = new BitArray(array);
byte[] byteArray = new byte[1];

bytes.CopyTo(byteArray, 0);

// byteArray[0] now has the value 0xCA

You can then write the array of bytes to a file, though, it's not going to be text so I don't know why you're trying to use a text file.

You can even just skip the bool[] array and just use the BitArray to hold all the bool values.
C#
BitArray bits = new BitArray(8);   // Holds 8 bits
bits[1] = true;
bits[3] = true;
bits[6] = true;
bits[7] = true;

byte[] byteArray = new byte[1];
bits.CopyTo(byteArray, 0);
 
Share this answer
 
Comments
computerpublic 29-Nov-17 17:08pm    
//No compiler error occur, but it gives me a LARGE empty output file that keeps building
//Also the program does not end.

using System;
using System.IO;
using System.Collections;
namespace Applica
{
static class Program
{
static void Main(string[] args)
{
bool tif = true;
bool[] array = new bool[8];
BitArray bytes = new BitArray(array);
const int BufferSize = 1024;
byte[] buffer = new byte[BufferSize];
FileInfo ap = new FileInfo("tempii.txt");
string filePath = ap.FullName;
string destinationPath = Path.Combine("C:\\check", Path.GetFileName(filePath));
using (Stream output = File.OpenWrite(destinationPath))
{
int amount = 1;
while (amount > 0)
{
for (int i = 0; i < 8; i++)
{
if (tif == true)
{
tif = false;
}
else if (tif == false)
{
tif = true;
}
array[i] = tif;
}
bytes.CopyTo(buffer, 0);//No compiler error occur, but it gives me a LARGE empty output file that keeps growing.
output.Write(buffer, 0, amount);//Also the program does not end.
}
}
}
}
}
Dave Kreskowiak 29-Nov-17 18:16pm    
Ummm...you don't need the entire "while" block in your code. The buffer you made has all of the data in it that needs to be written to the file and you can do that with ONE LINE OF CODE inside your "using" block.
    using (Stream output = File.OpenWrite(destinationPath))
    {
        output.Write(buffer, 0, buffer.Count);
    }

Of course, the two lines of code you put at the bottom have to be moved to above writing to the file for it to make any sense.

Seriously, LEARN TO USE THE DEBUGGER! It's there to help you understand what the code is doing!

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