Thread: Convert from Binary To Hex

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    9

    Convert from Binary To Hex

    Does any one have a small program which can convery a file (with binary data) to another file (in Hex format)
    Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There is no difference between a "binary file" and a "hex file". The only difference is on how you view it.

    Search the board (using the handy search link). This topic gets posted weekly.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    To convert a binary number to a hexadecimal number, use this:
    (make the read/write to the files yourself )
    Code:
    #include <math.h>
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    
    #define FirstDigit 48
    
    void BinToHex(char* BinData, char* HexData)
    {
       long int Number = 0;
       int BinLength = strlen(BinData);
    
       for(int i=0; i<BinLength; i++)
       {
          Number += ((BinData[BinLength - i - 1] - FirstDigit) * pow(2, i));
       }
    
       ltoa(Number, HexData, 16);
    }
    
    int main()
    {
       char* BinBuffer = "11110101";
       char HexBuffer[256];
    
       BinToHex(BinBuffer, HexBuffer);
       printf("Bin: %s\nHex: %s", BinBuffer, HexBuffer);
       getch();
       return 0;
    }
    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.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    9
    thanks all
    first I was trying to compile it using gcc and came with teh following errors:
    /var/tmp/ccb.aqqx1.o: In function `BinToHex':
    /var/tmp/ccb.aqqx1.o(.text+0x5c): undefined reference to `pow'
    /var/tmp/ccb.aqqx1.o(.text+0xd4): undefined reference to `ltoa'
    /var/tmp/ccb.aqqx1.o: In function `main':
    /var/tmp/ccb.aqqx1.o(.text+0x11c): undefined reference to `stdscr'
    /var/tmp/ccb.aqqx1.o(.text+0x120): undefined reference to `stdscr'
    /var/tmp/ccb.aqqx1.o(.text+0x124): undefined reference to `wgetch'

    I have already replaced:
    /*#include <conio.h>*/
    #include <curses.h>

    any ideas?

  5. #5
    Registered User
    Join Date
    Aug 2002
    Posts
    9
    quzah

    Let me make it a little clear
    I have a file that when I try to open with like say notepad shows some funny characters.
    All I wanted to do is to convert it into a format that I can use programs like Notepad to open the file up

    Thanks fo rthe help in advance

  6. #6
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by ladhnur
    quzah

    Let me make it a little clear
    I have a file that when I try to open with like say notepad shows some funny characters.
    All I wanted to do is to convert it into a format that I can use programs like Notepad to open the file up

    Thanks fo rthe help in advance
    if you have no clear concept of what you're dealing with (which is evident from your question), how will you know what to edit once you load it up?

    anyway, for your purposes, google yourself a "hex editor".
    hello, internet!

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    As stated, you don't know what you're dealing with so you have no simple way of changing it to "valid readable information". Consider the following:

    The binary value of:

    01101101101101100110110110110110

    What is that?

    Assuming 32bit integers, Is that:

    1 signed long integer?
    1 unsigned long integer?
    2 signed short integers?
    1 signed short integer + 1 unsigned short ingeter?
    1 unsigned short integer + 1 signed short ingeter?
    2 unsigned integers?
    1 signed short ingeter + 1 signed char + 1 unsigned char?
    1 signed short ingeter + 1 unsigned char + 1 signed char?
    4 unsigned chars?
    4 signed chars?
    Multiple cominations of signed and unsigned chars?

    I'm not even going to list all the possible combinations that you could have. If you don't know the format of the file, you can only guess or tiral and error your editing. You cannot simply turn it into valid readable data.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    9
    People please read what I write
    In my last post I used the sentence "Let me make it a little clear"
    that does not mean that I am not clear (like moi thinks). I know moi u may be a sen. member but that does not give right to talk like this so chill or keep quite.
    anyways coming back to the problem at hand....
    The application is basically going to take 8 binary digits (for e.g. 11110000) and convert it into hex (for e.g. 0xF0). Thats all

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by ladhnur
    People please read what I write
    In my last post I used the sentence "Let me make it a little clear"
    that does not mean that I am not clear (like moi thinks). I know moi u may be a sen. member but that does not give right to talk like this so chill or keep quite.
    anyways coming back to the problem at hand....
    The application is basically going to take 8 binary digits (for e.g. 11110000) and convert it into hex (for e.g. 0xF0). Thats all
    You still don't understand. You cannot simply convert it to hex and then change it and expect it to have the normal effect in whatever program you're dealing with. When you "convert to hex" you are simply taking one byte and transfering it to FOUR bytes of plain text: Example:

    You have the letter 'a'
    You convert it to hex:

    '0x61'

    Woopie! You now have four characters in your file instead of one. How is this bennificial? Your program whose data file you're reading doesn't know how to translate four ascii characters into a single decimal value or character.

    But what do I know, right?
    Code:
    while( (c=fgetc(inputfile) != EOF )
    {
        fprintf( outputfile, "0x%x", c );
    }
    If you would have followed my advice and taken five minutes out of your time and searched the board like I said, instead of being a smart ass, you'd have found your answer. But please, let me know what you think will happen works out with the above code. I'll be ... amused ... to hear the results.

    Quzah.
    Last edited by quzah; 10-28-2002 at 05:14 PM.
    Hope is the first step on the road to disappointment.

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you are looking for a method of displaying non printable characters on the screen, you can also try something like this (a mod of quzah's code):
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    while( (c=fgetc(inputfile)) != EOF )
    {
        fprintf( outputfile, "%c", isprint(c)? c : ' ');
    }
    This will ouput all characters, replacing the non-printable with a space.

    I'm not sure if this is what you're after... maybe you can provide an example of the input and output so we can advise you better.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by ladhnur
    People please read what I write
    In my last post I used the sentence "Let me make it a little clear"
    that does not mean that I am not clear (like moi thinks). I know moi u may be a sen. member but that does not give right to talk like this so chill or keep quite.
    anyways coming back to the problem at hand....
    The application is basically going to take 8 binary digits (for e.g. 11110000) and convert it into hex (for e.g. 0xF0). Thats all
    sorry if i talk hard, it's the way i am. i still don't get what you're trying to do. one byte on disk can be represented many ways:

    240 (unsigned char, base 10)
    -16 (signed char, 2's complement, base 10)
    -70 (signed char, using high bit as sign flag, base 10) <- dont see much
    0xF0 (unsigned char, base 16)
    11110000 (unsigned char, base 2)
    'ð' (character value of 240)
    360 (unsigned char, octal)

    however, all these (and more) representations have the same value in data. there is no "converting" from one to another. by some of what you said, it looks like you may be looking for a way to print a particular representation of these values. if so, why? if its to edit them in notepad and then save them back in to their original format, a hex editor does that for you much less painfully.
    hello, internet!

  12. #12
    Hello ladhnur.

    I don't understand exactly what you want to do here. Could you please explain to us (again) what you are aiming for?

    Here is some more info you may want to know:
    Notepad.exe does nothing except show exactly what is in a file, as characters. It has no way of knowing what kind of data it is, like possibly an int. if it was an int, then it would split it up into two (more likely than not) garbage characters (assuning 2 byte integers). And, unless you know the exat format of the file, neither can you.
    Last edited by Inquirer; 10-28-2002 at 07:28 PM.
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-16-2011, 11:59 AM
  2. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  3. Utility to Convert hex to binary
    By learnC in forum C Programming
    Replies: 3
    Last Post: 10-20-2005, 12:09 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM