Connecting Tech Pros Worldwide Help | Site Map

read a binary file bit by bit --- c#

  #1  
Old June 17th, 2009, 10:12 AM
Newbie
 
Join Date: Jun 2009
Posts: 2
Dear sir,

i am very new to c# and i request you to... please look into the problem

The following is the code(please see the code below:) which i uses to open the binary file,currently it works fine ,but now the problem is that 2 bytes are read in simultaneously and is being converted into an integer,but i need to read the data from the file in bits and finally convert 8 bits into binary numbers and then to decimal numbers? could you be able to give me some advice to read the file bit by bit ? is it ok if i read all the bytes into a an array and then try to convert it into a bit array?


with regards,
tom.

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3. using System.Collections;
  4.  
  5.  
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         R();
  11.     }
  12.  
  13.     static void R()
  14.     {
  15.         // 1.
  16.         using (BinaryReader b = new BinaryReader(File.Open("file.bin", FileMode.Open)))
  17.         {
  18.             // 2.
  19.             // Position and length variables.
  20.             int pos = 0;
  21.             // 2A.
  22.             // Use BaseStream.
  23.             int length = (int)b.BaseStream.Length;
  24.             while (pos < length)
  25.             {
  26.                 // 3.
  27.                 // Read integer.
  28.                 int v = b.ReadInt32();
  29.                 Console.WriteLine(v);
  30.  
  31.                 // 4.
  32.                 // Advance our position variable.
  33.                 pos += sizeof(int);
  34.  
  35.                 // 5.
  36.                 // Seek to our required position.(int pos1 had to be found out )
  37.                 //int pos1 = 0;
  38.                 //b.BaseStream.Seek(pos1, SeekOrigin.Begin);
  39.  
  40.                 // 6.
  41.                 // Read the next 2000 bytes or required number of bytes int the byte array(int required had to be found out ).
  42.                 //int required = 20000;
  43.                 //byte[] by = b.ReadBytes(required);
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.                 //.8 convert to bit array
  52.                 //BitArray myBits = new BitArray(by);
  53.                 byte[] value = { 0x11 };
  54.                 BitArray myBits = new BitArray(value);
  55.  
  56.  
  57.             }
  58.  
  59.             Console.ReadLine();
  60.         }
  61.     }
  62. }
  63.  
----------------------------------------------------------------------------------------------------------------------------------
and the following is the data which i gets when i try to open this file with visual studio:
S€DŠ4}]ݝ܍MŒM[m4]\|LLŒ|Œ\L,ϼн=]|ܾ˜\qO-<< Kټƻ˯+•€Kj[{TONU^{o‹{{Œ몼ǽŒ9Œh<™ͬ]$
9*]}-œm]ݲݪ*š]‰]€ֽ]
>*\Œˬ"‡ǬkŬK|#̻˯+•~iݝ ݝmݽmܲˆV=5mLL|μŒ Ϭ

Last edited by PRR; June 17th, 2009 at 11:13 AM. Reason: Please post code in [code] [/code] tags.
  #2  
Old June 17th, 2009, 10:54 AM
Administrator
 
Join Date: Sep 2006
Posts: 12,084

re: read a binary file bit by bit --- c#


You are mixing things up a bit here. What is the nature of your binary file?
Using b.ReadInt32(); will attempt to read an int from the file not a bit. Obviously you need to be sure that there is an int in there at the current reading position for it to work. Better read this article.
Reply