473,503 Members | 11,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Binary data extraction

8 New Member
hello everybody,
i'm trying to read binary data from an input file and store their bit by bit representation in an int array for further manipulation:
- using the file.read(buffer,1) to retrieve my first byte of data
- using unsigned char mask (for ex 0x80 = 1000 0000) i'm trying to AND it with my buffer in order to get my MSB, but i dont know how to retrieve the remaining bits. On the other hand my buffer type should it be char or unsigned char cause i repeat my data is recieved in binary from a transciever.
Oct 26 '07 #1
4 2507
amitpatel66
2,367 Recognized Expert Top Contributor
hello everybody,
i'm trying to read binary data from an input file and store their bit by bit representation in an int array for further manipulation:
- using the file.read(buffer,1) to retrieve my first byte of data
- using unsigned char mask (for ex 0x80 = 1000 0000) i'm trying to AND it with my buffer in order to get my MSB, but i dont know how to retrieve the remaining bits. On the other hand my buffer type should it be char or unsigned char cause i repeat my data is recieved in binary from a transciever.
Could you please POST your code for reference?
Oct 26 '07 #2
freeurmind
8 New Member
Could you please POST your code for reference?
Yes sure:

ifstream InputFile("Input.txt",ios::in | ios::binary);
unsigned char buffer; // buffer is one byte of my data
unsigned char mask = 128; // = 1000 0000
int binarr[8]; //here is where my binary representation is stored
Bool mybit; //this should represent the bit i'm trying to retrieve
InputFile.get(buffer);
for ( int i =0; i<8 ; i++)
{
mybit = buffer & mask;
if (mybit)
binarr[i] = 1;
else
binary[i] = 0;
buffer<<1 ; // this should be shifting my buffer 1 bit left
}


But still this aint working i'm not sure of the bitwise operators
and something weird happening cause i tried any buffer like
buffer = 157 but my binarr is getting filled with 0012FF54 and dont know where it got these numbers
Oct 26 '07 #3
amitpatel66
2,367 Recognized Expert Top Contributor
Yes sure:

ifstream InputFile("Input.txt",ios::in | ios::binary);
unsigned char buffer; // buffer is one byte of my data
unsigned char mask = 128; // = 1000 0000
int binarr[8]; //here is where my binary representation is stored
Bool mybit; //this should represent the bit i'm trying to retrieve
InputFile.get(buffer);
for ( int i =0; i<8 ; i++)
{
mybit = buffer & mask;
if (mybit)
binarr[i] = 1;
else
binary[i] = 0;
buffer<<1 ; // this should be shifting my buffer 1 bit left
}


But still this aint working i'm not sure of the bitwise operators
and something weird happening cause i tried any buffer like
buffer = 157 but my binarr is getting filled with 0012FF54 and dont know where it got these numbers
Why dont you clear your bin array and then run the code again.
Oct 26 '07 #4
freeurmind
8 New Member
Why dont you clear your bin array and then run the code again.
hey thnx for ur reply
i got it working i guess, it was very stupid, i had to use buffer <<=1 and not buffer<<1 although i'm not totally understanding the differene btw the 2 operators i guess << would just temporary shift the buffer but wouldnt change the value
anyway thnx for ur help for the time being
Oct 26 '07 #5

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

Similar topics

35
3597
by: Troll | last post by:
Hi, I need to write a script which reads some data and reports the findings. Just to give you an idea the structure is similar to the following. Data input example: HEADING 1 **********...
27
2322
by: geskerrett | last post by:
I am hoping someone can help me solve a bit of a puzzle. We are working on a data file reader and extraction tool for an old MS-DOS accounting system dating back to the mid 80's. In the data...
7
1865
by: Drake | last post by:
Well, I'm stuck in legacy land and I need a helping hand. We're trying to give some modern value-added functionality to a circa-1985 fortran proggie. The program produces a binary file, by...
8
2101
by: Bruce Lee | last post by:
Hi I have the following 2 questions: 1.How to write binary data to a text file in C? I have a number like 10001010110001 say, I need to put it in the text file so that I can see the corresponding...
1
1921
by: Jason Huang | last post by:
Hi, To make it short, how do we do the data extraction to MSWord using ASP.Net C#? Any help will be appreciated. Jason
2
2963
by: Jason Huang | last post by:
Hi, Would someone show me how to do the data extraction to Excel in ASP.Net using C# web form? I am not familiar with VB, so I am asking someone to help me out! Any help will be appreciated. ...
8
4114
by: Ronin | last post by:
I'm very new to C++ coming in from using C# and the like. I've read a lot of good posts about binary files but I'm still missing a few key ingredients that will help me with the following problem....
2
2490
by: Wayne Marsh | last post by:
Hello, Is it considered sane/good practice to write a global operator for the insertion and extraction operators of an fstream in binary mode to serialize a binary class, or are they strictly...
2
1571
by: 0658 | last post by:
Public Class Form1 Dim n As Integer = 0 Dim m As Integer = 0 Dim s As String Dim x As String Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As...
0
7207
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,...
0
7361
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5602
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 project—planning, coding, testing,...
1
5026
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...
0
4693
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...
0
3183
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1523
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 ...
1
749
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
403
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...

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.