473,568 Members | 2,762 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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]\|L LŒ|Œ\ L,ϼн =]|ܾ˜ \qO-<< Kټƻ˯+• €Kj[{TONU^{ o‹{{Œ몼ǽŒ 9Œh<™ͬ ]$
9*]}-œm]ݲݪ*š]‰]€ֽ]
>*\Œˬ "‡ǬkŬK|# ̻˯+•~i ݝݝmݽ mܲˆV=5 mLL| μŒ Ϭ
Jun 17 '09 #1
1 8242
r035198x
13,262 MVP
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.
Jun 17 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

17
10464
by: Guyon More | last post by:
what is the difference? if I open a text file in binary (rb) mode, it doesn't matter... the read() output is the same.
7
5051
by: KantKwitDansin1 | last post by:
I have a file "a.dat" containing 10^4 32 bit binary numbers. I need to read in these numbers and deterimine if they are prime. The primality part I will have to figure out later, but I was wondering if there is anyone out there that can help me in reading in the binary numbers. Basically, read in 32 bits and thats the first number, read in...
3
18956
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its original form. The problem is tha the binary source that I extract from the text file seems to be diferent from the source I saved. Here is my code: 1)...
1
2260
by: Quinn | last post by:
Hi all, I have some binary files in the following format: text line 1 text line 2 .... text line N end of text single in binary 1 single in binary 2 single N EOF
3
18422
by: utab | last post by:
Dear all, What are the advantages of binary files over text files? I would like to search for a specific value of a variable in an output file, I was doing this lately by the string library functions of the language. But wondering the advantages of searching and reading from a binary file. If the files get too large, I guess the advantage...
6
5792
by: =?Utf-8?B?VGhvbWFzWg==?= | last post by:
Hi, Is it possible to read a file in reverse and only get the last 100 bytes in the file without reading the whole file from the begining? I have to get info from files that are in the last 100 bytes of the file and some of these files are 600Mb -1 GB in size. I am getting "outofMemory.." exceptions on the largest files and the other files...
6
7359
by: ericunfuk | last post by:
Hi ALL, I want to read a binary file(it's pic.tif file, I guess it's binary file?), then write it to a new file), I have several questions about this process: When I use fread() to read a chunk of the file into a buffer, when it encounters the end of the file, will the EOF indicator be put into the buffer automatically just as an...
6
32852
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line). Is there some way to burst read the whole file and later "extract" each line? Thanks in advance, Thomas Kowalski
4
3466
by: Matrixinline | last post by:
Hi All Here is my problem I am using a Unicode project and I tried to read the File like sPath = LPCTSTR; FILE* oFp = _tfopen(sPath,L"r"); while(!feof(oFp))
6
342
by: zl2k | last post by:
hi, there I have a appendable binary file of complex data structure named data.bin created by myself. It is written in the following format: number of Data, Data array Suppose I have following data.bin (3 Data appended to 2 Data): 2, data0, data1, 3, data0, data1, data2
0
7693
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main...
0
7916
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7660
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2101
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.