|
-
June 30th, 2004, 12:11 PM
#1
ascii to binary
I need to convert an ascii file to binary. Essentially just reading in the ascii data and printing it out to another file in binary. I don't know where to start with the conversion. I have searched through other threads and sites and I find the information either confusing or contradictory.
I am reading and writing the files normally, it's just the conversion I need help with so... Please Help!
Thanks!
-
June 30th, 2004, 12:18 PM
#2
ASCII is just a way to encode text.
Binary is not a specific type, it simply means "uninterpreted" data. Any file could be treated as a binary file.
What is the problem you are trying to solve?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
June 30th, 2004, 12:29 PM
#3
I am required to read in ascii text, convert the text to binary and then print the binary out to another file.
-
June 30th, 2004, 12:47 PM
#4
I am required to read in ascii text, convert the text to binary and then print the binary out to another file.
Are you sure of what you want ? It's not clear for us... Who or what do require this ? Is this a person ? So explain that it doesn't seem so useful that you can't do any other way. It's a program ? Then, I don't know...
-
June 30th, 2004, 12:52 PM
#5
It's to convert a file my program recieves that will convert the text to binary output to be sent to another program.
-
June 30th, 2004, 12:57 PM
#6
I know what Keylee717 is saying...., she wants to take the binary of an ASCII and turn it into binary...
so "ASCII" -> "0100000101010011010000110100100101001001"
notice that "0100000101010011010000110100100101001001" is going to have a binary form of itself as well..., but she wants it in this form so that when it is opened it will look like this...
but keylee, you realise that text is already in binary, everything is in binary..., ASCII is just an encoding of binary...
basically what you want to do is do the same thing as this page does..., http://nickciske.com/tools/binary.php
I hope this helps,
Byan
Last edited by Byan; June 30th, 2004 at 01:01 PM.
-
June 30th, 2004, 01:09 PM
#7
Well, yes, that is exactly what I need to do, but how do I go about doing it?
-
June 30th, 2004, 03:12 PM
#8
Well, one method would be to "brute force" it. The left most bit is multiplied by 2^0, the next bit is multiplied by 2^1, etc. Then they are all added together.
Code:
0x1110 = (1*2^3)+(1*2^2)+(1*2^1)+(0*2^0)
0x1110 = 8+4+2+0 = 14
Now, if your ASCII is just one long run-on binary stream, you're going to have to figure out where you want to break it up (for example, every 32 characters), and then apply this.
Viggy
-
July 2nd, 2004, 05:13 AM
#9
Another method is shifting:
Code:
char chCrt;
...
for(int i=0; i<8; i++)
{
byte bit = ((chCrt << i ) >> (7 - i));
PrintOneBit(bit);
}
Or you can do somehting like:
Code:
struct bits{
byte b7:1;
byte b6:1;
byte b5:1;
byte b4:1;
byte b3:1;
byte b2:1;
byte b1:1;
byte b0:1;
};
#define ASCIITOBINARY(ch) (ch.b7#ch.b6#ch.b5#ch.b4#ch.b3#ch.b2#ch.b1#ch.b0)
...
char chRead;
struct bits bit;
memcpy(&bit, &chRead,sizeof(chRead));
fprintf(f,"%s",ASCIITOBINARY(bit));
Or just print the bits in any way you want.
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
|