Thread: hex to decimal

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Creating a program to convert hex to decimal is not difficult what-so-ever. Though, I'm unsure of how to convert a single hex character to decimal since I'm used to working in pairs. For instance 'FF' or '23'.

    Just for a hint, here is how it works.

    Step 1
    Code:
    Take the value of first hex character
    Take an array and loop 16 times left shifting by four digits each time.
    Note: If you do happen upon a hex value that you entered, break the loop.
    Step 2
    Code:
    Take the value of the second hex character and add that to the previously obtained value.
    Do this 16 times also with a loop.
    Note: If you do happen upon a hex value that you entered, break the loop.
    If you would like for me to show a sample example, I can.

    Edit: You could use scanf() or sprintf() to get the %x. If so, you can always view this thread.

    If you have further questions, please feel free to ask.


    - Stack Overflow
    Last edited by Stack Overflow; 11-10-2004 at 08:58 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    I'd think you could roll your own with sscanf.
    Code:
    #include <stdio.h>
    
    char *foo(char *dst, const char *src)
    {
       int i, j = 0;
       for ( i = 0; src[i]; ++i )
       {
          if ( src[i] != '%' )
          {
             dst[j++] = src[i];
          }
          else
          {
             unsigned ch;
             if ( sscanf(&src[++i/* skip '%' */], "%2x", &ch) != 1 )
             {
                return NULL;
             }
             dst[j++] = ch;
             ++i; /* with the loop's increment, this will skip the 2 hex chars */
          }
       }
       dst[j] = '\0';
       return dst;
    }
    
    int main(void)
    {
       const char text[] = "C%2B%2B%20or%20C%3F";
       char result [ sizeof text ];
       puts(text);
       if ( foo(result,text) )
       {
          puts(result);
       }
       return 0;
    }
    
    /* my output
    C%2B%2B%20or%20C%3F
    C++ or C?
    */
    Last edited by Dave_Sinkula; 11-11-2004 at 10:12 AM. Reason: Changed text string (spaces to %20) and output.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  2. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 02:14 AM
  3. Converting decimal to hex
    By cobrakidd in forum C++ Programming
    Replies: 9
    Last Post: 02-06-2003, 11:37 AM
  4. about decimal to hex progam
    By Abdi in forum C Programming
    Replies: 2
    Last Post: 06-01-2002, 07:08 PM
  5. Decimal vs. Hex
    By -leech- in forum C Programming
    Replies: 6
    Last Post: 02-18-2002, 12:51 PM