hex to decimal

This is a discussion on hex to decimal within the C Programming forums, part of the General Programming Boards category; I'm working on a CGI program in C and I've hit a snag. when a url is returned it is ...

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    hex to decimal

    I'm working on a CGI program in C and I've hit a snag. when a url is returned it is usally encoded with special characters where any characters following the '%' char is encoded in hex. My problem is I'm not very mathematically inclined and would be interested to understand how to convert hexadecimal chars to decimal. for instance given the follwing str.
    Code:
    thisismeC%2B%2B%10
    how would i go about converting it, I've searched the web and came across some code
    Code:
    char upcase(char c)
    {
    	if (c >= 'a' && c <= 'z')
    		c -= 32;
    	return c;
    }
    
    int hex2dec(char c)
    {
    	if (c >= '0' && c <= '9')
    		return (int) c - '0';
    	else
    	{
    		c = upcase(c);
    		if (c >= 'A' && c <= 'F')
    			return (int) (10 + c - 'A');
    		else
    			return 0;
    	}
    }
    I'm not quite sure how it works. I'd appreaciate some help or at least
    directions *ptr.
    thanks.

  2. #2
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    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 07: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.

  3. #3
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    You may also look at this one

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    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 09: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.*

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Let strtol() do the work for you:
    Code:
    itsme@itsme:~/test/src$ cat foo.c
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
      char hex[] = "2b";
    
      printf("%ld\n", strtol(hex, NULL, 16));
      return 0;
    }
    itsme@itsme:~/test/src$ ./foo
    43
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thank you guys very much, you've given me more than enough information. Very nice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 03-24-2006, 10:26 AM
  2. hex to binary,hex to decimal
    By groovy in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 01:14 AM
  3. Converting decimal to hex
    By cobrakidd in forum C++ Programming
    Replies: 9
    Last Post: 02-06-2003, 10: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, 11:51 AM

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