Thread: hex string to integer...

  1. #1
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    this is one of the k&r exercises that ive been stuck on. i was trying to do this manually and not using a stdio.h function. i wasnt aware this function existed. and may i add k&r doesnt assign exercises very well. i guess they expect you to already know whats out there. the use of pointers wasnt discussed and yet i had to figure out the basics of them and use it to do an exercise.

    Code:
    #include<stdio.h>
    
    unsigned int htoi(char *s);
    
    int main()
    {
    	char	s[10];
    	int	i;
    	int	c;
    
    	for (i = 0; i <= (10 -1) && (c = getchar()) != EOF  && c != '\n'; i++)
    		s[i] = c;
    
    //do i need to add a '\n' and a '\0' here?  or will this suffice and put the '\0' at the end? 
    						
    	printf("%d\n",htoi(s));
    
    	return 0;
    }
    
    unsigned int htoi(char *s)
    {
    	unsigned int n;
    	int r;
    
    	r = sscanf(s, "%x", &n);
    	return n;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    and may i add k&r doesnt assign exercises very well. i guess they expect you to already know whats out there. the use of pointers wasnt discussed and yet i had to figure out the basics of them and use it to do an exercise.
    You really aren't supposed to use the standard library for this exercise, that defeats the purpose. The exercise assumes that you only know what has been shown up to that point, which is certainly possible:
    Code:
    /* Assume ASCII */
    int htoi(char s[])
    {
        int i = 0; /* Iterate over s */
        int n = 0; /* Built up number */
    
        /* Remove "0x" or "0X" */
        if ( s[0] == '0' && s[1] == 'x' || s[1] == 'X' )
            i = 2;
    
        while ( s[i] != '\0' )
        {
            int t;
    
            if ( s[i] >= 'A' && s[i] <= 'F' )
                t = s[i] - 'A' + 10;
            else if ( s[i] >= 'a' && s[i] <= 'f' )
                t = s[i] - 'a' + 10;
            else if ( s[i] >= '0' && s[i] <= '9' )
                t = s[i] - '0';
            else
                return n;
    
            n = 16 * n + t;
            ++i;
        }
    
        return n;
    }
    My best code is written with the delete key.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >this is one of the k&r exercises that ive been stuck on.

    The following links may be of interest.
    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.*

  4. #4
    Registered User xion's Avatar
    Join Date
    Jul 2003
    Posts
    63
    thanks for the response guys/girls.
    prelude thanks for the informative post. i had trouble getting the logic down on how to calc from hex to int. by seeing your code i understand all but one thing.

    Code:
    n = 16 * n + t;
    doesnt 16 have to be squared the 3rd time this loop goes around? then cubed the 4th time around? just to make sure.

    man i hope this logic gets easier as i go along.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >doesnt 16 have to be squared the 3rd time this loop goes around? then cubed the 4th time around?
    No, that's not necessary. My htoi is basically the atoi function from pp. 43. The only real difference is working with base 16 and handling A-F as well as 0-9.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. hex string to integer...
    By Ruchikar in forum C Programming
    Replies: 3
    Last Post: 10-16-2002, 06:58 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM