Thread: Binary to ascii and vice versa

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Question Binary to ascii and vice versa

    OK, im trying to write a c++ proggie to convert binary bits to ascii chars one that has been via proram back to a WORKING executable or whatever it was. I have found this formula from another board.....

    X = "your binary char"

    A = (X & 11110000 >> 4) + 65;
    B = (X & 1111) + 65;

    //A and B are two ASCII charaters

    X= (A - 65) << 4 + (B - 65); // To go back to binary


    First question is this correct. Second question if A and B are ascii chars then defining them as char should be fine right? As for the binary bit do i use .get and a char or unsigned char?? or would it be a .read and some other type??

    I tried to write a quickie to run the formula and get a warning of possible loss of bits for A when a is char and X is char, ill mark that. Here is what i have, sorry its not user friendly.
    PHP Code:
    #include<iostream.h>
    #include<fstream.h>




    void char_to_binary(void)
      {
     
    char binary_file[256];
     
    char hex_file[256];
     
    char binary_bit;
     
    char high_bit;
     
    char low_bit;

    cout << "Input the CORRECT path to a created file:  "<< endl;
    cin >> binary_file;
    ifstream input_file(binary_file);
    cout << "Input a path and filename for the output:  " << endl;
    cin >> hex_file;
    ofstream output_file(hex_file);


    while(!
    input_file.eof())
        {
        
    high_bit input_file.get();
        
    low_bit input_file.get();
        
    binary_bit = (high_bit 65) << + (low_bit 65);
        
    output_file.put(binary_bit);
        };

    input_file.close();
    output_file.close();
    };


    void binary_to_2char_bits(void)
    {
    char binary_file[256];
    char hex_file[256];
    char binary_bit;
    char high_bit;
    char low_bit;

    cout << "Input the CORRECT path to a binary or ""any"" file i guess:  "<< endl;
    cin >> binary_file;
    ifstream input_file(binary_file);
    cout << "Input a path and filename for the output:  " << endl;
    cin >> hex_file;
    ofstream output_file(hex_file);

    while(!
    input_file.eof())
        {
        
    binary_bit input_file.get();
        
    high_bit = (binary_bit 11110000 >> 4) + 65;
    //here is warning of lost bits in conversion, also warning that object is long//
        
    low_bit = (binary_bit 1111) + 65;
        
    output_file.put(high_bit);
        
    output_file.put(low_bit);
        };

    input_file.close();
    output_file.close();

    };

    void main(void)
    {
    binary_to_2char_bits();
    cout << "first convert done..." << endl;
    char_to_binary();
    cout << "Program done.";

    }; 
    Now be forgiving i am a newbie. But this does SOMETHING? but the finished product had hardly any data so i think i AM losing bits. I need to know what to define the high_bit, low_bit, and binary_bit and what read and write funcs to use. The first func MUST be pure ascii chars when done, readable in notepad ect...

    Also I am confused in the formula. I understand what its doing and the bitwise and operator but i am not understanding what the redirection " >> 4" is doing there. If someone could tell me that too.

    I know its alot in one post but its late and i am tired and anxious to get this to work as its the last part of my programs functionallity. Thanks alot ill chack back in less than 24hours.
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    A couple of thoughts...

    Surprisingly, C++ is not very "good" at binary! Decimal, Octal, and Hex are easy. People who actually work with individual bits (like me... I'm a hardware guy) use Hex.

    Any "conversion" between number bases is done ONLY during input/output. As you probably know, everything in memory is in binary. So, you can enter a number in Decimal, Octal, or Hex, and then display it in Decimal, Octal, or Hex. Oh, you can display the ASCII character too.

    You can read a file in "binary format" and then display the file in ASCII without any "conversion". The binary file format is is a way of reading/writing without regard to the actual format. You can write any byte (00-FF) to any location. With text format, you're looking for linefeads, carrage returns, etc. You could describe the binary file format as "dumb" or "don't care", or "random". The ASCII format is "Smart" or ...well... formatted.

    i am not understanding what the redirection " >> 4" is doing there
    Ah, that means shft right 4 places. I didn't study the code that carefully, but that's how you would "look at" the high nybble. Each nybble (4 bits) converts to one Hex character. You may already know that, but that's what makes converting between binary and hex easy (for humans).

    [EDIT]
    ">>" and "<<" are bitwise operators... I should have said "4 bits" instead of "4 places". That is... 2 << 1 = 4 (not 20)

    CODE TAGS
    Take a look at the post at the top of the board that says "Posting Code, Read This First"

    Lastly, if you REALLY want a program that will take a binary number from the keyboard and display it in any base, I have one, but it's at home and I'll have to post it later.
    Last edited by DougDbug; 03-26-2003 at 06:30 PM.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    Cool, ect.....

    Thanks Salem and DougDBug.....

    That answers alot of my questions. I knew do a proggie like this wouldn't be to hard but since my only resources are to lame books and the internet not everything is as descriptive as i like. I know the problem is that i used those "11110000" and "1111" Ill see if i can get it to work now.

    Perhaps I am missing something though. You say I can simply write a binary input as acii chars without doing any converting?? That might be best. I dont really care the format its in ...
    ....my goal here is to be able to take binary input/bytes and run them through functions i have made to run on txt files, the theoreticly if i undo everything i did and convert it back to its ORIGINAL form =) so an executable would run again ect......

    IF you have any suggestions of a very efficient way of doing this, aka better than mine, let me know. My main problem seem to be getting a file back to its original bytes without ANY alterations.....

    Much thanks and ill let you know if that code i posted works....
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  4. #4
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595
    I realize my posting this is a bit late...but oh well


    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    ..hmmmmmm

    I fixed the binary number problem and added some () to fix possible precidence problems and it now compiles <the code at top of post> without any warnings at all.

    BUt when i try to convert an exe at 1.5 megs to the chars i get like 28 chars and of course it doesnt convert back to a working app if its only 28bytes. Oh i just remembered someone said all converting must be done at input. Does that mean using variables wouldnt even work???? OR is the whole trying to use char for everything not working or what??

    Im sure if i saw it done i could figure it out but suprisingly i cant find any examples of c progs that do the whole conversion thing. Idealy I would actually like to output the binary to hex, does that mean i could just go like input_file.get() >> hex >> variable(would this be a char or char array?) or do i need to use .read.

    Maybe i should just try it huh =)
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  6. #6
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    I don't know if this has already been said but the function itoa() could be VERY useful.
    If you ever need a hug, just ask.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    i get like 28 chars
    Probably caused by this: while(!input_file.eof())

    I dunno if it's clear from the FAQ ('cause I just skimmed the FAQ) but, eof is just a number which is only found at the end of a file saved in ASCII format, but might be found at position 29 in your exe file.

    My main problem seem to be getting a file back to its original bytes without ANY alterations.....
    If you're not doing encryption, there is probably no good reason to read the file, manipulate it and then re-write it as it was (?). Reading a file doesn't mess it up.

    With a large file, you should probably NOT read the entire file into an array. (Is that what you are doing?) You should read one byte (or a few) at a time and write it to a new (encrypted?) file.

    Also, I assume that you realize not every possible byte converts to a printable ASCII charater. An exe file will contain lots of non-ASCII.

    Now, if you REALLY want to do conversions between various bases and ASCII, study the attached file. (It doesn't do any file I/O)

    I have another example that does bitwise manipulation. Let me know if you'd like to see it.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    Ok, i now get why it stops during the file using get, since get isnt really for anything but char/txt files. Um how might i use read then, to get byte by byte, read doesnt seem to work when i screw around with it.

    Well, anyways, sure encryption whatever, actually im working on file compression and i hve a working model that works with text files and i want to use it with binary files by writing a binary files bits in some sort of acsii compatable form i can later put back to binary. Hex bits in ascii would work but how do i get them back once they are in txt chars. Im just no getting it all right. I know there is a way to do it. I just dont know how.

    THanks for help guys, gals
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I dunno if it's clear from the FAQ ('cause I just skimmed the FAQ) but, eof is just a number which is only found at the end of a file saved in ASCII format, but might be found at position 29 in your exe file.<<
    That isn't clear from the FAQ, because your statement isn't true in any way. EOF isn't stored in the file at all, it's a return code from the functions that read files. In most cases, EOF is an int with value -1, which is outside the range of a char.

    >>char binary_bit;
    >>binary_bit = input_file.get();
    The .get() element returns an int, so using a char will not allow you to trap the EOF value correctly.
    http://www.cppreference.com/cppio_details.html#get
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Question Really

    thenrkst,
    Sorry if I posted false information.

    Hammer,

    EOF isn't really stored in the file? I always thought it was the last byte in the file... That's what your above link implies (?)
    will read in one character at a time- when we get to the end of the file, a special character EOF (End-Of-File) will be read
    Are you saying that this function [ input_file.eof() ] is not scaning throught the file data looking for EOF? Does it get it's result from the OS?

    Just because EOF is -1 and outside the range of any character does not mean that you won't find -1 in the middle of an exe file. It seems that this function [ch = fin.get() ] (from the link)could be fooled by a non-text file... Right?

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Really

    @DougDbug:
    Lots of questions....

    >>Sorry if I posted false information.
    No problem, not even experienced people are right everytime

    >>EOF isn't really stored in the file?
    No, it isn't.

    >>I always thought it was the last byte in the file... That's what your above link implies
    Hmm, I suppose it does imply it, imho it's badly worded.

    >>Are you saying that this function [ input_file.eof() ] is not scaning throught the file data looking for EOF? Does it get it's result from the OS?
    From the OS, yes, or whatever is doing the actual IO.

    >>Just because EOF is -1 and outside the range of any character does not mean that you won't find -1 in the middle of an exe file.
    Now you're confusing two different things. A -1 int is not a -1 char, they are two different values. Strange I know, but its true. To try and explain, I've added a new FAQ entry today. I haven't had time to vet it properly, so excuse any typos, but it should give you an idea of what's going on here.

    >>It seems that this function [ch = fin.get() ] (from the link)could be fooled by a non-text file... Right?
    No, see the new FAQ entry.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    Sorry Dougdbug i never even looked at the base.cpp you sent me. I think ill figure it out once i play around with output a little bit. I just have to experiment to teach my self what i dont know. I have lousy books for reference. Thanks, and thanks for clearing up that eof thing. I hadnt messed with it yet but i would have gone the wrong direction if someone wouldnt have pointed that out.

    -thenkrst
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  13. #13
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    I still cant get a binary file to input all the way to the end byte by byte, Ive tried .read with char and int and long and tried .get, i dont know if its even inuting right thouhg because i cant do anything until i actually can get it to input all th way through. .eof works, i think its the pointer in the file itself or something.

    In a file that has 1,000,000 bytes i get like20 or 30 then hits end, even if i dont use .eof the pointer of the file hits the end and repeats the same value over and over. I dont get it.

    HELP! Please refer to the code i posted to keep answers relevant since really what i want to do is what the code says wheter im doing it wrong or not.
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  14. #14
    Registered User
    Join Date
    Mar 2003
    Posts
    30

    New code not needed

    Ok, first sorry for the pathectic spelling of my posts. I usually post after 20 hours of being awake and i dont catch all that un-understandable slurs i leave.

    Second, besides some () ( so i dont have to try to spell parentheses), there no change in the code but trying read with a buffer of various sorts or using get, and switching the var types i use for input. BY reading the faq i seem to understand that a char is 1 byte and int is 2 bytes"at least thats what i remember, i may be wrong".

    But no matter how many different things ive tries the simple program doesnt seem to parse byte by byte, it just goes wherever and ends up misisng 90% of the data. Tha actuall acsii convertion appears to work to me. (mind you i actaully havent tried using the c version of "f" functions and tried following the file pointer. I dont have a C++ reference that tells me how to control the file pointer in the ifstream functions??

    SO my problem is i cant seem to get it to read the entire file right, i get around 20-30 bytes no matter what binary file i try to read. And there is no obvious reason for this that i can see. If i run it with a txt file it run fine regardless of what form (int long char) i use.
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

Popular pages Recent additions subscribe to a feed