Thread: Convert Binary to Hexadecimal

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    5

    Question Convert Binary to Hexadecimal

    So after entering 8 bits binary number, I need to convert it into Hexadecimal. And I got this far, but get errors.

    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    #define CONVERSION_ERROR -1
    #define NO_ERROR 0
    
    
    
    
    int main (void) {
    	char byte[9], numeral[2], hex[3];
    	short bit;
    	    int i, decimal_value = 0;
    	    char hex_look_up[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
    
    
    	    numeral[1] = 0;
    	    for (i=7, bit=0; i>=0; i--, bit++) {
    	        printf("Enter 1 or 0 for bit number %d: ", bit);
    	        gets(numeral);
    	        if (numeral[0] == '0' || numeral[0] == '1') byte[i] = numeral[0];
    	        else {
    	            printf("Error on input: not a 1 or 0\n");
    	            exit(1);
      }
              printf("\n");
          }
          byte[8] = 0;
        printf ("The binary value in the byte is %s\n", byte);
    int convertBinToHex (const char * byte, char * hexNo);
        int error = NO_ERROR;
        int lower_end = convertBinToDec(byte,7,4);
    	if(lower_end>9)
    	hexNo[0] = hex_look_up[10-lower_end];
    	int upper_end = convertBinToDec(byte,3,0);
    	if(upper_end>9)
    	hexNo[1] = hex_look_up[10-upper_end];
    	hexNo[2] = '\0';
        return error;
    printf("Hexadecimal value is %s",hexNo);
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    First, tell us what errors do you get
    * Compiler errors? If so, copy-paste them all, with line numbers.
    * Run-time errors? If so, what input do you give? What output do you get? What output do you expect to get?
    * Does it crash? What messages do you see? How far does it seem to get?

    Then, fix your indentation and formatting. If your code is hard to read, it's easy to make mistakes and hard to find or fix them. It should look more like this:
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    
    #define CONVERSION_ERROR -1
    #define NO_ERROR 0
    
    
    int main (void) {
        char byte[9], numeral[2], hex[3];
        short bit;
        int i, decimal_value = 0;
        char hex_look_up[6] = {'A', 'B', 'C', 'D', 'E', 'F'};
    
    
        numeral[1] = 0;
        for (i = 7, bit = 0; i >= 0; i--, bit++) {
            printf("Enter 1 or 0 for bit number %d: ", bit);
            gets(numeral);
            if (numeral[0] == '0' || numeral[0] == '1') byte[i] = numeral[0];
            else {
                printf("Error on input: not a 1 or 0\n");
                exit(1);
            }
            printf("\n");
        }
    
    
        byte[8] = 0;
        printf ("The binary value in the byte is %s\n", byte);
    
    
        int convertBinToHex (const char * byte, char * hexNo);
        int error = NO_ERROR;
        int lower_end = convertBinToDec(byte,7,4);
    
    
        if (lower_end>9)
            hexNo[0] = hex_look_up[10-lower_end];
    
    
        int upper_end = convertBinToDec(byte,3,0);
        if (upper_end>9)
            hexNo[1] = hex_look_up[10-upper_end];
    
    
        hexNo[2] = '\0';
        return error;
        printf("Hexadecimal value is %s",hexNo);
    }
    Next, compile at the maximum warning level (e.g. for gcc use the -Wall flag). If you do, you should get the following:
    Code:
    $ make foo
    gcc -Wall -ggdb3 -std=c99 -O0 -o foo foo.c -lm -lpthread -lrt
    foo.c: In function ‘main’:
    foo.c:34:3: warning: implicit declaration of function ‘convertBinToDec’ [-Wimplicit-function-declaration]
    foo.c:36:5: error: ‘hexNo’ undeclared (first use in this function)
    foo.c:36:5: note: each undeclared identifier is reported only once for each function it appears in
    foo.c:15:10: warning: unused variable ‘decimal_value’ [-Wunused-variable]
    foo.c:13:29: warning: unused variable ‘hex’ [-Wunused-variable]
    make: *** [foo] Error 1
    1. The error ('hexNo' undeclared) actually prevents your code from compiling. If you want to use a variable, you must declare it.
    2. The first warning is because convertBinToDec doesn't exist. Also, you only have a prototype for convertBinToHex, and the prototype should be outside any functions. Usually near the top of the file, just before you actually define your functions.
    3. The last 2 warnings are because

    Also, note that the last printf will never execute, since it's after the return statement.

    Work on all that, and when you get back with specifics on your error, I'll be able to help you more.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    5
    I only got
    Code:
    ‘hexNo’ undeclared (first use in thisfunction)
    I'm not sure about this part...please help me
    Code:
     intconvertBinToHex (constchar* byte, char* hexNo);    int error = NO_ERROR;
        int lower_end = convertBinToDec(byte,7,4);
    
    
        if (lower_end>9)
            hexNo[0] = hex_look_up[10-lower_end];
    
    
        int upper_end = convertBinToDec(byte,3,0);
        if (upper_end>9)
            hexNo[1] = hex_look_up[10-upper_end];
    
    
        hexNo[2] = '\0';
        return error;
        printf("Hexadecimal value is %s",hexNo);
    }
    

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by dnguyen8 View Post
    I only got
    Code:
    ‘hexNo’ undeclared (first use in thisfunction)
    That's because you didn't turn up the warning level on your compiler. You should do that, compiler warnings are signs that you're doing something you shouldn't, and that is possibly dangerous.

    It would probably be easiest if you converted the binary digits to an int, then used sprintf to print the hex value of that int to a buffer/string.

    Read up on converting binary to decimal. There are some great tutorials on the web: https://www.google.com/search?q=conv...ary+to+decimal.

    Figure out how you would do it yourself, with paper and pencil. That will be the basis for your algorithm. If you can't do it yourself, you can't program a computer to do it.

    EDIT:
    * You need to give more specifics about the problem. How long can the binary string be? Are you allowed to use sprintf?
    * Also, explain what it is you don't understand about that part. It seems fine, at least on the surface. What don't you get? It's possible convertBinToDec is broken, meaning even if that section is right, it wont work. Garbage In, Garbage Out as they say. Can you show us convertBinToDec? How about you print the value of upper_end and lower_end for debugging purposes to make sure they're correct.
    * What input are you giving and what output are you getting?
    Last edited by anduril462; 10-11-2013 at 09:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to convert a binary table to hexadecimal value
    By thefirstone92 in forum C Programming
    Replies: 12
    Last Post: 10-03-2013, 10:56 AM
  2. trying to convert hexadecimal to decimal
    By chrissy2860 in forum C Programming
    Replies: 7
    Last Post: 01-23-2012, 04:11 PM
  3. convert string to hexadecimal
    By nocturna_gr in forum C Programming
    Replies: 3
    Last Post: 12-11-2007, 04:45 AM
  4. ASCII convert to Hexadecimal value?
    By ashish.malviya in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 12:45 AM
  5. stacks to convert from decimal to hexadecimal
    By drdodirty2002 in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2004, 12:24 AM