Thread: Reading binary files and writing as text

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

    Question Reading binary files and writing as text

    I am a begginer C++ writer. I understand the basics but shy away from the structures and objects at the moment.

    I am looking for the simplest way to read any binary file...(i understand all files are stored as binary) and i want to output that file and or work with it as "zeros and ones" in text.

    I will eventually need to restore the file back to its original binary state and have it funcion with no alteration or loss.

    Also is it possible to read ANY file and output it as text hex. I would assum so since its just another way of writing numbers.


    Help me with this and you may soon see me on TV as a very rich man. Haha. Thanks.
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You could open a file in binary mode, read a byte at a time, the use the LOBYTE and HIBYTE macros to get the two nibbles (hex letters) from the byte.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    30
    Sounds like that might work for getting it.

    Two favors, what header are the HIBYTE and LOBYTE maros found in and would a similar method be able to restore the file back to its original binary format without change?

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

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    windows.h I believe.

    Otherwise you could write your own using bitshifting and bitmasks:
    Code:
    #define LOBYTE(w) (BYTE)(w & 0x0F)
    
    #define HIBYTE(w) (BYTE)((w >> 4) & 0x0F)
    BYTE is for windows. char should also do the job

    To remerge two nibbles, use bitmasking again:
    Code:
    #define MERGE(hb, lb) (BYTE)((hb << 4) | lb)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

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

    Thumbs up Thank you Magos

    Like i said I am a begginer so i'll have to do a little reading up on the macro to fully understand it. But I'm pretty sure this will be just what I need. You see i was playing around with text file encryption for fun and came across an interesting concept for compressing/reducing the size of text files, or dictionaries to be exact =) and began playing with shifting characters around and trying to find patterns of repeating data in the files.

    I am now wanting to toy with hex and base 2 binary because mathematically the less characters the more chance of pattern repetition. So using the hex style your method would provide will actually be less processor intensive, and will work fine.

    Thank you much.

    I am however still looking for a way to actually write any file directly into a binary string that can be worked with as text characters. I suppose if i have the hex bits i can just convert it to binary numbering system. But thats just an extra processing step.

    Thanks again for a quick reply Magos
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

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

    A word about binary...

    Binary numbers are not "easy" in C++. For example, you can't (directly) put a binary number in your source code. Also, cin and cout don't (directly) support binary. I was absolutely shocked to find this out a few months ago! So, if you're comfortable with hex, use hex! Or just get comfortable with hex and use it!

    directly into a binary string that can be worked with as text characters.
    I'm not sure what you're doing, but usually you can work with integers as "numbers", and display them in decimal, octal, or hex without converting them to "text" (text = ASCII?).
    Also is it possible to read ANY file and output it as text hex. I would assum so since its just another way of writing numbers.
    YES! And, If you have a hex editor you can view any file in hex, and you can change the hex values. Microsoft Visual C++ has a hex editor. You change the filename extension to .hex and open it with Visual Studio.

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

    Wink Cool and bummer too.

    I am goind to keep replying to this because I want to get all the info i can sap out of you guys.

    Ok, to clarify WHAT I actually wnt to accomplish with this question is in simplest terms i know...

    I want to take a program i've written the manipulates text and then can take the jibberish and put it back to original form, and be able to use it for any file.

    So....I want to be able to read a file in a binary stream, and take it and output its bits in a hex or binary form that is compatable with char and string functions. Seems taking it apart wont be that hard. My main concern is i need to take the hex numbers or binary strings "0001001" ect.. and put them back in the pure form they came form so if the file was perhaps and exacutable before. It would execute as normal again when im done puting it back together =)


    Thanks for the input so far. If anyone has any good methods for me in file convertion like this, namely restoring back to its pure binary form from hex or a binary string. Please gimmie an idea what direction to go. I would like to explore as many methods as possible.

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

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

    Ecryption? (Secrect code?)

    Nobody can tell you how to restore the file unless we know how you've "manipulated" it!!!

    The most common method of simple encryption / decryption is to use bitwise "'exclusive or". You can look into "boolian logic" and "bitwise operations" if youi're not familiar with exclusive or. Exclusive or is a reversable operation... You XOR each character byte with a key byte. Now your characters are now "encoded". If you XOR again with the same key, they are decoded. Of course this isn't very secure because you only have to try all 256 possible keys (0-FF hex).

    Now, if you want to try something really simple to get started, you could add one to each byte. This would change "A" to "B", "B" to "C", etc. Then subtract one to decode. Ummm... you might want to limit that to bytes that convert to ASCII characters.

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

    Exclamation

    Trust me what Im actually doing doesn't matter =)
    I lied anyways.


    When i want to reassemble the file it will already be in the EXACT format it was AFTER I used whetever methode to convert it from binary to hex or whatever. SO what im looking for is ideas and recommendations on converting binary files (any file) (or standard binary stream input form C++) into a text format of bit info comptable with char and string functions so i can run fucntions i wrote designed for text files on any file.
    "...son, what is this COUT << on my birthday card mean?"
    expected ";" line 12, 54, 63, 73....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. writing and reading files
    By oldie in forum C Programming
    Replies: 1
    Last Post: 07-03-2008, 04:54 PM
  2. Reading text files
    By Xinco in forum C++ Programming
    Replies: 20
    Last Post: 01-09-2007, 01:24 PM
  3. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  4. reading and writing to files in windows
    By Dohojar in forum Windows Programming
    Replies: 8
    Last Post: 06-26-2003, 02:07 AM
  5. writing to binary files....
    By jverkoey in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2003, 09:58 PM