CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: ascii to binary

  1. #1
    Join Date
    Apr 2004
    Location
    New Hampshire
    Posts
    68

    Question 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!

  2. #2
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    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...

  3. #3
    Join Date
    Apr 2004
    Location
    New Hampshire
    Posts
    68
    I am required to read in ascii text, convert the text to binary and then print the binary out to another file.

  4. #4
    Join Date
    Jun 2004
    Posts
    7
    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...

  5. #5
    Join Date
    Apr 2004
    Location
    New Hampshire
    Posts
    68
    It's to convert a file my program recieves that will convert the text to binary output to be sent to another program.

  6. #6
    Join Date
    Jun 2004
    Posts
    19
    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.

  7. #7
    Join Date
    Apr 2004
    Location
    New Hampshire
    Posts
    68
    Well, yes, that is exactly what I need to do, but how do I go about doing it?

  8. #8
    Join Date
    Feb 2002
    Posts
    4,640
    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

  9. #9
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360
    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured