-
August 11th, 2009, 02:13 PM
#1
Converting from string("01000001") to a byte and back.
AHHHHH! I give up. How do I convert a strings of ones and zeros into a byte that I can write to a binary file??? I've tried everything I can think of. Here is what i have so far:
Code:
string val = "01000001"; //Binary for letter 'A'
unsigned char a = 00000000;
//cout << a << endl;
unsigned char MASK = 1 << 0;
//cout << MASK << endl;
for (int i = val.size()-1; i >= 0; i--) {
a <<= 1;
if(val.substr(i,i+1)=="1"){
a |= MASK;
}
}
cout << a << endl;
fyi i want to write the file like this, so i need it in char arrays.
char * output; //load data, etc. etc.
writeFile(output, length, "output.txt");
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|