473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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

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|μŒ Ϭ
Jun 17 '09 #1
1 8223
r035198x
13,262 8TB
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
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
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...
3
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...
1
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
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...
6
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...
6
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...
6
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)....
4
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.