473,394 Members | 1,658 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,394 software developers and data experts.

Converting a String to Binary

I need some help, my C++ coding skills are a little old and rusty.

I need to read in a series of 8-bits stored in a text file. How do I read in these values and get the proper result?

For example, the file looks like this:

01101011 00101011 010101101 ....

That is saved in a .txt file as ASCII characters, but I need to manipulate them as bytes. Can anyone provide any help? Thanks for your time.
Feb 10 '07 #1
4 1881
RRick
463 Expert 256MB
Actually, don't worry about the binary stuff, your file is in ascii which is just what C++ and the IO objects like. 8 bit bytes is the format that ascii uses. Usually, a .txt file of ascii characters is viewable with an editor (i.e. vi or notepad).

Take a look further down these postings for a tutorial on C++ IO. Open the file, read lines of text into a string and (probably) print it out to the screen. Loop until you've read everything.

Cheers
Feb 10 '07 #2
Thanks but that's not what I mean. The file is a series of 1's and 0's. If I read it in as an int I get a base 10 number with any leading zeros dropped. So 0011 would be eleven, not binary 3. If I read it in as a series of chars I get ASCII 48 and 49. What I need is to be able to read in the eight binary digits and save them so that I can perform a series of bit-wise XOR's, AND's, etc.

Can anyone provide help? I guess I could brute force it but there has to be a more elegant way.

Thanks
Feb 10 '07 #3
Ganon11
3,652 Expert 2GB
The only way I could think of is storing these integers in a series of int arrays of size 8, then processing them that way. I know there's a way to read binary files, but I'm not sure...something like ios::bin comes to mind...
Feb 10 '07 #4
horace1
1,510 Expert 1GB
If the binary data is 0 and 1 characters you could read the 0's and 1's as sequences of 8 bit values converting them to ints, e.g. to read one byte
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.     int data=0;
  8.     char bit;
  9.     for(int i=0;i<8;i++)
  10.       {
  11.        // read a 0 or 1 skipping whitespace
  12.        cin >> ws >> bit;
  13.        // shift existing data one left and add current bit value
  14.        data = (data << 1) + (bit - '0');
  15.        }
  16.       cout << "data " << hex << data << endl;
  17.       system("pause");
  18. }
  19.  
when run gives
00000101
data 5
Press any key to continue . . .
Feb 10 '07 #5

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

Similar topics

2
by: Mariusz Sakowski | last post by:
I'm writing class which will be able to store large numbers (my ambition is to make it able to operand on thousands of bits) and perform various operations on it (similiar to those available with...
8
by: Marius Cabas | last post by:
Hi, I'm a beginner so don't shoot ;) I'm reading a wave file into a byte and I'm trying to convert the result to String but the converted string is altered, so if I'm generating a new wave file...
7
by: dlarock | last post by:
I wrote the following to do an MD5 hash. However, I have a problem (I think) with the conversion from the Byte MD5 hash back to string. Watching this through the debugger it appears as if the...
2
by: Arlyn | last post by:
I was wondering how I would convert String data into a binary. I want to store and the binary data in a mssql table. When I retrieve the binary data I want to convert it from a binary array to a...
3
by: Wallace | last post by:
Hai, Can anyone tell how to convert an object into string? Please help me.... urgent.. Thanx in advance...
2
by: LittlePython | last post by:
I am trying to create a hexstring of a NT4 user account sid which I can in turn use to query an exchange 55 database. I believe I need to convert a binary sid to a hex string. ADsSID com object...
16
by: manmit.walia | last post by:
Hello All, I have tried multiple online tools to convert an VB6 (bas) file to VB.NET file and no luck. I was hoping that someone could help me covert this. I am new to the .NET world and still...
2
by: DBuss | last post by:
OK, I'm reading a multicast socket. It attaches fine, reads fine, all of that. The problem is that while some of the data I get is normal text (ASCII String), some of it is Binary Integer. ...
3
by: Schmacker | last post by:
Hi there, I'm trying to teach myself some things about random file I/O with binary, and have come across a task that I am unsure I know how to approach. Currently I write out a bunch of...
0
Frinavale
by: Frinavale | last post by:
Convert a Hex number into a decimal number and a decimal number to Hex number This is a very simple script that converts decimal numbers into hex values and hex values into decimal numbers. The...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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
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
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...
0
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...

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.