-3

I'm asking a simple question but i can't find the easiest way to do that, my application read a small file, Simply the 8 bytes word "uploaded" read from just 8 byte file, will be a binary array of 0,1... or list of true-false and the length is 8*8= 64 bits, NOW I already have this 64 bits in a array of string and also list of Boolean, the code below is faster and i just need to edit the code to give me a 8-bits per time not 1 byte..**** with MemoryStream

 string path = openFileDialog1.FileName;
 byte[] file = File.ReadAllBytes(path);
 MemoryStream memory = new MemoryStream(file);
 BinaryReader reader = new BinaryReader(memory);

 for (int i = 0; i <= file.Length - 1; i++)
 {

 byte result = reader.ReadByte();

 }

After editing this code, i just need to write back this bits

01110101-01110000-01101100-01101111-01100001-01100100-01100101-01100100 to be uploaded

to bytes then Write it back to a valid file.?? i'm really tired because i see many methods to write a bytes array as a file but not a bits... i'm tired because i can't find a way out !!

  • possibly [BitArray](http://msdn.microsoft.com/en-us/library/system.collections.bitarray(v=vs.110).aspx) is what you need – Grundy Dec 29 '14 at 20:35
  • A byte *is* 8 bits. If you want each bit of a byte, one at a time, you can use `BitArray`, or can use bitwise operators, but your question as its written is unclear. There also doesn't appear to be any point in your usage of `MemoryStream` and `BinaryReader` from what you've posted. – Preston Guillot Dec 29 '14 at 20:37
  • how i can do something like BinaryWriter.Write() but in bitArray – Joe Marcel Dec 29 '14 at 20:37

1 Answers1

1

You can use the BitArray constructor accepting a byte array:

var bitArray = new BitArray(new byte[] { result });

Then you can call bitArray.Get(n) to get the bit at position n of the result byte.

As for your edit, the code can be reduced to this:

string output = "";
byte[] fileBytes = File.ReadAllBytes(path);
var bitArray = new BitArray(fileBytes);

// Loop over all bits in the bitarray, containing all bytes read from the file.
for (int i = 0; i < bitArray.Length; i++)
{
    output += bitArray.Get(i) ? "1" : "0";

    // Output a dash every 8 characters.
    if ((i + 1) % 8 == 0)
    {
        output += "-"
    }
}

// Write `output` string to file.
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Really Thank you so much for your help, so what i'm thinking is true?? i already have the code above to read byte each time.. then i just need to compare the bits then read another 1 byte, how i can reverse BACK the bits array to be bytes array then write the bits i have read to be a valid file?? – Joe Marcel Dec 29 '14 at 20:45
  • I don't know, some example input and output (in your question, not a comment) would really help. – CodeCaster Dec 29 '14 at 20:46
  • Thank you for your help again, but i need a array of 64 bit, so after finish reading all the bytes. i'll get a bitArray of 64?? or just the last byte? – Joe Marcel Dec 29 '14 at 20:58
  • thank you again, but i swear this is not a homework, i'm working on something greater, something in my work. but you believe me if i told you i have a 1 year working on this project, i do everything harder than this.. but i have a two problem's i can't fix.. can i just tell you that's problem's not to fix it. just i can comfort after this hard hard hard work, please listen to my issue?? – Joe Marcel Dec 29 '14 at 21:06
  • I understand, but sometimes hard work is all it takes. Now try my code and try to explain what doesn't work about it. – CodeCaster Dec 29 '14 at 21:08
  • visual studio debug stopped working when i load a image file.. around 1Mb or 2Mb...,, that's why i need to use MemoryStream.. becuase when i use the code above in my question.. when i load 200Mb .. there's no problem.. maybe there's no answer to my question " how i can edit the MemoryStream Code above .. so it can give me a boolean or binary not byte!, any suggestion? – Joe Marcel Dec 29 '14 at 22:15
  • Yeah I think you should use a BinaryReader to read chunks of bytes from the file as opposed to File.ReadAllBytes at once. – CodeCaster Dec 29 '14 at 22:17