Binary to Hex Conversion

This is a discussion on Binary to Hex Conversion within the C Programming forums, part of the General Programming Boards category; Dear friends, I am trying to write a function that will convert an integer to a hex string. I would ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    Binary to Hex Conversion

    Dear friends, I am trying to write a function that will convert an integer to a hex string. I would like to use the switch method. The code below is my work in progress, the logic i'm trying to follow is:

    take the decimal integer 'n' and divide by 16
    the remainder 'm' will form the first hex digit - hexstring[]
    I want to use '0' to represent '1', '1' to represent 'A" and on until remainder 15 (F).
    Then i want to take the quotient and divide by 16 and the remainder will form the second hex digit, next to last.
    i want to repeat this process until my string is filled up and if it stop with a quotient equal to 0, fill the rest of the hex string with '0'.



    Code:
    int itox( char hexstring[], int n) {
       int m, b, flag;
       b = (m / 16);
       m = m - (b * 16);
       for (m = 0;m > 0; m++)
       switch (m) {
    
    case '1' :
       m = 01; 
       break;
    
       case '2' : 
       m = 02;
       break;
    
       case '3' : 
       m = 03;
       break;
    
       case '4' :
       m = 04;
       break;
    
       case '5' : 
       m = 05;
       break;
       
       case '6' : 
       m = 06;    
       break;
       
       case '7' : 
       m = 07;
       break;
       
       case '8' : 
       m = 08;
       break;
       
       case '9' :
       m = 09;
       break;
       
       case '10' :
       m = A; 
       break;
    
       case '11' : 
       m = B;
       break;
       
       case '12' : 
       m = C;
       break;
    
       case '13' : 
       m = D;
       break;
       
       case '14' :
       m = E;
       break;
      
       case '15' : 
       m = F:
       break;
       
       default:
       flag = 0;
       break;
       }
    
      return (hexstring[m++]);
      }
    
    }
    could someone help me with my syntax, when I pass on a decimal i have an invalid digit "8" in octal constant

  2. #2
    Registered User
    Join Date
    Jul 2006
    Posts
    14

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Why is the subject of this thread "binary to hex conversion"? Your function is screwy enough that I can't really tell what's going on, but it sounds like you just want:
    Code:
    char buffer[BUFSIZ];
    sprintf(buffer, "%X", n);
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    re:

    sorry for the screw up code,

    can't use printf because it is not allowed.

    Code:
    int itox( char hexstring[], int n) {    /* takes in an integer n */
       int m, b, flag;
       b = (m / 16);                               /* i divide by 16 */
       m = m - (b * 16);                        /* then because b is an int i can find the remainder by subtracting this number from the original number*/
       for (m = 0;m > 0; m++)              /* then i want to start a for loop saying if m is greater then zero, increment count by testing the cases below */
       switch (m) {                               */
    /* finally after checking the cases, put them into a string and return that. */
    if you have any better suggestiongs of how i should rewrite so is not so screwed up.
    Last edited by elrookie; 06-26-2007 at 10:13 AM.

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,468
    where do you assign something to hexstring?
    reminder operator is %
    where do you use n?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Some things to consider:

    You're using m uninitialized. It starts off as garbage because you never give it an initial value.
    Code:
    b = (m / 16);  
    m = m - (b * 16);
    ...can just be rewritten:
    Code:
    m = m % 16;
    b is unnecessary.

    Next, your for loop will never run:
    Code:
    for (m = 0;m > 0; m++)
    You initialize m to 0 and then the condition checks if m is greater than 0, which obviously it won't be. Your entire loop will just be skipped over.

    This brings us to your switch statement. What if you simply did this instead:
    Code:
    char hexdigits[] = "0123456789ABCDEF";
    hexstring[index++] = hexdigits[m];
    As long as m is between 0 and 15 (inclusive), it will assign the correct hex digit to your hexstring.

    Finally, don't forget to terminate hexstring with a '\0' after the loop to turn it into a string.
    Last edited by itsme86; 06-26-2007 at 10:35 AM.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Whats m? Unless it could be anything when you are calculating what b is since its not initialized.

    To loop through the string you could pass a pointer to the string in eg:
    Code:
    int itox( char* hex_str)
    You will then need to loop through all the characters in the string something like:
    Code:
    while(*hex_str)
    {
        //get your hex value here
        *hex_str++;
    }
    If you want to split the char into 2 nibbles, then use the modulus operator like vart said. Eg:
    Code:
    left = this_char / 16;
    right = this_char % 16;
    Hope that helps.

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    17

    re:

    thank you for your suggestions! a lot

    I have a question about this piece of code:

    Code:
    char hexdigits[] = "0123456789ABCDEF";
    hexstring[index++] = hexdigits[m];
    where does index++ come from?


    thanks for the suggestion Mike about the pointer but we haven't covered that in class yet, so I am not familiar with that method yet.


    but how will it know that if it gets a remainder 11 it suppose to be B? because of the order of where it is position in the hexdigits [] ?


    Code:
    /* so far i got this i'll continue working on this to get what i want*/
    
    
    int itox( char hexstring[], int m) 
    
    
    {
       int m, flag;
       m = m % 16;
       for (m = 0 ;; m++)
       char hexdigits[] = "0123456789ABCDEF"
       hexstring[index++] = hexdigits[m];
       {  
       return (hexstring[m]);
    }
    Last edited by elrookie; 06-26-2007 at 11:09 AM.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    It starts at 0, each time through the loop it just increments by 1. You just build the string one character at a time that way.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  3. Hex to binary conversion and storing in nybble
    By shoobsie in forum C Programming
    Replies: 14
    Last Post: 10-25-2005, 02:51 AM
  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, 09:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21