Thread: binary to hex

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    7

    binary to hex

    hey,

    first of all am new to C programming ! am trying to write a program that changes a binary 8 or 16 bit number into hex, i used to the select case in my program, my question is how can i make it recognise the whole 8/16 digit binary number i enter and not only the 4 bits that are identified in the look up table.. thank you


    Code:
    #include <stdio.h>
    
    int main ();
    void bintohex (int);
    
    
    int main ()s
    {
      int digit;
    
      scanf ("%d", &digit);
      bintohex (digit);
      return 0;
    }
    
    
    void bintohex (int digit)        /* print out bintohex code */
    {
      switch (digit)
      {
        case 0000 : printf ("0");
        break; 
        case 0001 : printf ("1");
        break;  
        case 0010 : printf ("2");
        break; 
        case 0011 : printf ("3");
        break; 
        case 0100 : printf ("4");
        break;
        case 0101 : printf ("5");
        break; 
        case 0110 : printf ("6");
        break; 
        case 0111 : printf ("7");
        break; 
        case 1000 : printf ("8");
        break;
        case 1001 : printf ("9");
        break;
        case 1010 : printf ("A");
        break; 
        case 1011 : printf ("B");
        break; 
        case 1100 : printf ("C");
        break; 
        case 1101 : printf ("D");
        break;
        case 1110 : printf ("E");
        break;
        case 1111 : printf ("F");
        break;
    
    
      }
      printf ("\n\n");
    }

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    scroll down to my post
    http://cboard.cprogramming.com/showt...hlight=lecture
    it's c++, but conversion is trivial

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
        case 0000 : printf ("0");
        break; 
        case 0001 : printf ("1");
        break;  
        case 0010 : printf ("2");
        break; 
        case 0011 : printf ("3");
        break; 
        case 0100 : printf ("4");
        break;
        case 0101 : printf ("5");
        break; 
        case 0110 : printf ("6");
        break; 
        case 0111 : printf ("7");
        break;
    There is a problem with these as they will be interpreted by the compiler as octal values. You are effectively saying this:
    Code:
        case 0 : printf ("0");
        break; 
        case 1 : printf ("1");
        break;  
        case 8 : printf ("2");
        break; 
        case 9 : printf ("3");
        break; 
        case 64 : printf ("4");
        break;
        case 65 : printf ("5");
        break; 
        case 72 : printf ("6");
        break; 
        case 73 : printf ("7");
        break;
    Values with leading zeroes are interpreted as octal in your compiled code, i.e. 0100 is seen as 100 (octal) which is 64 (decimal).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    7
    oh thanks i didn't know that, where can i start form then writing a code?

  5. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Also, %d wants an integer in decimal, that is base 10. So if you enter 101 you will get 101 in decimal, not 5.
    You should retrieve a string and then process it four bytes a time (four bits = one hex character) and convert it to decimal then print out a number if it's in the interval 0 to 9 or print out a letter if it's in the interval 10 to 15.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  2. Printing Hex as Binary
    By Mystic_Skies in forum C Programming
    Replies: 6
    Last Post: 11-22-2004, 04:18 PM
  3. 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
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Is binary HEX?
    By Budgiekarl in forum Tech Board
    Replies: 11
    Last Post: 11-23-2003, 09:02 AM