473,651 Members | 2,538 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(buffe r,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 2520
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(buffe r,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("Inpu t.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(b uffer);
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("Inpu t.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(b uffer);
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
3644
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 ********** ColumnA ColumnB ColumnC ColumnD ColumnE
27
2338
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 files, the text information is stored in clearly readable ASCII text, so I am comfortable that this file isn't EBCIDIC, however, the some of the numbers are stored in a format that we can't seem to recognize or unpack using the standard python...
7
1869
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 itself no problem... each record needs to be converted into std::vector<mystruct> I'm having a helluva time with the binary-->(pod)datatype conversion. Although I've referred to C++ containers, iostreams
8
2114
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 characters when I open the file. 2. How to extract bits from a byte. I think it is using bit operations, but for example, how would I extract the 2nd most significant bit in a byte?
1
1928
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
2971
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. Jason
8
4120
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. I have a binary file that has a specific number of floats per row. I need to read each float into a float array for processing by another block of code (calculations). Each row gets read into this array and processed before the next row gets...
2
2500
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 meant for formatted text input and output? Let's imagine, for example, that I had a standard Windows BMP file (I am aware that C++ has no concept of a BMP - this is simply putting my question in a simple context). If I wanted to load it into a...
2
1580
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 System.EventArgs) Handles Button1.Click Dim webadd As String webadd = ""
0
8347
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, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8694
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8571
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4143
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
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
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.