Click here to Skip to main content
15,886,032 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone.. i hope the title of my question is clear to all..
@First.. i have done to create an application to upload a files then convert it to string array of Binary 0,1.. the code below
C#
string filename = openFileDialog1.FileName;
FileInfo FileInf = new FileInfo(filename);
int len = openFileDialog1.FileName.Length;

byte[] DA = File.ReadAllBytes(filename);
string s = string.Join("", DA.Select(x => Convert.ToString(x, 2).PadLeft(8, '0')));
string[] a = s.Select(c => c.ToString()).ToArray();
for (int i = 0; i <= a.Length-1 ; i++)
{
   if (a[i] == "0")
  {
    Doing something....
  }
   else if (a[i] == "1")
  {
    Doing something....
  }
}


Now i have a problem with the code above.. is it's very slow and it can't take any file large than 1Mb maybe.. or less than.. i know it's because the file converted to X8 Bytes!! but that's what i need..

So.. What i search on internet, is to (Read from MemoryStream) this's gonna be faster..
and the 2nd better code is
C#
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; i++)
 {
  byte result = reader.ReadByte();
  MessageBox.Show(result.ToString());
 }


Now, The Second Code i feel it's good because i tried to load about 1GB file, and there's no lag or stopped working message!!.. that's very good. but this code gave me a decimal value and i don't need that..
All of what i need is to make my application loaded an large file without any slowest reading And Also convert byte after byte to binary index's (8 bits in 8 index's)
For ex..
First byte in the file is 117..
i need to make it directly in the 8 index's
0
1
1
1
0
1
0
1
After done.. read Another 1 byte.. TO THE END.

Please accept my best regards, & i'm ready to explain more details..
Posted
Updated 19-Dec-14 17:26pm
v4
Comments
Sergey Alexandrovich Kryukov 19-Dec-14 19:43pm    
What is "string binary array"? :-)
—SA
Majd ROG 19-Dec-14 19:50pm    
Hello, what i mean in that.. i have a 1 byte for ex..
And i need to make an 8 index's array of 0 1 string
if i look into array index [0],, i want to see the 0 bit for ex..
0,1,1,0,1,0,1,0
So.. @ the final thing if i have 100 bytes file..
After i convert it to binary array.. i must got 100x8 bit.. so that will be 800 byte
>>>>>>
On other way.. i need to read the file bits 0,1.. and each bit in single index
not 8 bit (1 byte in 1 index..) NO NO!
Sergey Alexandrovich Kryukov 19-Dec-14 21:26pm    
Not clear at all. Can you finally explain everything properly, without making us pulling information from you?
—SA
Majd ROG 19-Dec-14 23:08pm    
Ok,,
I need someone to edit the 2nd code (using MemoryStream) to read an file in binary 0,1,0,0,1,0,0,1 etc.. to the end of file..
but the binary values isn't integer!! i need that values in array of string so i can compare the index of the for loop with it..
For ex..

for (int i = 0; i <= BinaryArray.Length -1 ; i+=2)
{
if (BinaryArray[i] == "0")
{
do something..
}
else if (BinaryArray[i] == "1")
{
do something else..
}

so. i need to edit the 2nd code my question.. because when i use it i got an decimal value.. and i need a binary!!

sorry but i'm trying to be clear, and i hope so..
BillWoodruff 20-Dec-14 0:44am    
I do not understand what you are trying to do. You show code to read a file into an Array of Bytes. If you need to access the values of the bits in each Byte in that Array, why do you need a string representation in binary one's-and-zeroes ?

Please try to state your goal as clearly as possible.

1 solution

Just in case these utility methods may assist you:
C#
public static List<bool> BinaryStringToBitsAreOnes(string theString)
{
    return theString.Select(ch => ch == '1').ToList();
}

public static List<string> StringToCharsAsBinary(string theString)
{
    return theString.Select(ch => Convert.ToString(ch, 2).PadLeft(8, '0')).ToList();
}
Sample test: in some method or EventHandler:
C#
List<string> results = StringToCharsAsBinary("abc");

List<bool> bools = BinaryStringToBitsAreOnes(results[0]);
// sample output as viewed in VStudio Output Window:
> ? results
Count = 3
    [0]: "01100001"
    [1]: "01100010"
    [2]: "01100011"
> ? bools
Count = 8
    [0]: false
    [1]: true
    [2]: true
    [3]: false
    [4]: false
    [5]: false
    [6]: false
    [7]: true
>
Edit: I think the System.Collections.BitArray may assist you; example:
C#
public static BitArray ByteToBits(byte theByte)
{
    return new BitArray(new byte[]{theByte});
}

// sample use in some method or EventHandler

byte aByte = 63;

BitArray bArray = ByteToBits(aByte);

foreach (var val in bArray)
{
    Console.WriteLine(val);
}
This would give an output like this:
True
True
True
True
True
True
False
False
Note that if you create a BitArray with multiple Bytes, you will get an Array containing eight boolean flags per Byte.

See the MS documentation on BitArray for information about its various constructors, and uses: [^].

Edit: how to use a BitArray that holds the results of evaluating many Bytes.
C#
public static List<bool> BytesToListOfBools(byte[] theBytes)
{
    // BitArray implements IEnumerable !
    return new BitArray(theBytes).Cast<bool>().ToList();
}

// sample use: execute in a method or EventHandler

byte[] bytes = new byte[] {0, 2, 4, 8, 16, 32, 64, 128};

List<bool> listOfByteAray = BytesToListOfBools(bytes);

StringBuilder sb = new StringBuilder();

for (int i = 0; i < listOfByteAray.Count; i += 8)
{
    var >Output of the above:<pre lang="text">False, False, False, False, False, False, False, False
False, True, False, False, False, False, False, False
False, False, True, False, False, False, False, False
False, False, False, True, False, False, False, False
False, False, False, False, True, False, False, False
False, False, False, False, False, True, False, False
False, False, False, False, False, False, True, False
False, False, False, False, False, False, False, True
 
Share this answer
 
v4
Comments
Majd ROG 20-Dec-14 7:26am    
Thank you @BillWoodruff for your assist, actually you'r true..
As you say.. i need to use the 2nd code. using "MemoryStream" to read a file into an Array of Bytes.. then i create a for loop to file length byte after byte.
BUT MY PROBLEM WITH THE SECOND CODE IS.. the code gave me a byte in decimal..
And all of what i need is to want to access to the access the values of the bits in each Byte in that array .. bits after bits ..

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; i++)
{
byte result = reader.ReadByte();
//THIS CODE GIVES ME A ONE BYTE EACH TIME BUT IN DECIMAL !!!!!!
//I NEED TO ACCESS THE BITS IN EACH BYTE LIKE THAT "0,1,1,0,0,0,0,1"
//ACCESS TO INDEX [0] = 0
//Index [1] = 1

}

This code i need to use but i just need to edit the code, so i can read byte after byte but in binary, NOT IN DECIMAL, then.. access to the 8 bits in the 1 byte!
BillWoodruff 20-Dec-14 8:47am    
See the use of System.Collections.BitArray added to my answer.
Majd ROG 20-Dec-14 15:43pm    
Thank you so much @BillWoodruff for your help, i'll try in a few hours the code you gave me, just a question, i'll use inside my MemoryStream code, System.Collections.BitArray code you gave me, so MemoryStream read 1 byte then
i'll use your code to convert the 1 byte to array of 8 bits each time.
i think this is good. right? i'll try and let you know.. thank you so much again
BillWoodruff 20-Dec-14 17:07pm    
Keep in mind that you can assign multiple Bytes at a time into a BitArray, not just one, as shown in my example. For each Byte in the BitArray you will have eight entries in the BitArray.

To access the Bytes in the BitArray one byte at a time, you will need to take advantage of the fact that the BitArray implements IEnumerable: unfortunately, BitArray does not provide any method to grab a certain number of elements at-a-time.

I've added another example above to demonstrated accessing each 8 bit values in a multi-byte BitArray.

Note that if you are dealing with a very large file converted to a ByteArray and then to a BitArray, you may approach the limit of the size of Collection in System.Collection, and that is something you should research.

See comment on use of BitArray in this Project Euler problem:

http://www.mathblog.dk/sum-of-all-primes-below-2000000-problem-10/

When you transform each bit in a BitArray into a bool, you consume one Byte, so you are multiplying size by a factor of eight !

Be warned: I have not fully thought this through !
Majd ROG 20-Dec-14 17:29pm    
Thank you so much @Billwoodruff, this is a great work from you..
I'll try and i'll keep in touch with you.
Just a question, if i dealing with large file like 20,50Mb maybe 100Mb? can i use i MemoryStream Byte-After-Byte?? to make no issues ??
or i must search for a better than MemoryStream BitArray??

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