my_atoi

This is a discussion on my_atoi within the C Programming forums, part of the General Programming Boards category; Hello, I have developed my own atoi version, that converts ASCII Hex to Integer. When ASCII hex are mentioned in ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    38

    my_atoi

    Hello,

    I have developed my own atoi version, that converts ASCII Hex to Integer. When ASCII hex are mentioned in lowercase, code works perfectly.

    If string contains uppercasase, I troed to convert it to lowercase, but code is throwing unhandled exception.

    Here is the code.
    Code:
    unsigned short my_atoi(char *p)
    {
    	unsigned int i,j;
    	int count = 2;
    
    	i= j = 0;
    	
    	while( j < count)
    	{
    		// If you would like to have flexibility to have both upper case 
    		// and lower case ASCII HEX.
    		if ( *(p+j) >= 'A' && *(p+j) <= 'F' )
    		{
    			k = *(p+j)+32;
    			*(p+j) = k;
    			printf("Value of p = %d\n",k);
    		}
    
    		if ( *(p+j) >= '0' && *(p+j) <= '9' )
    			i = i * j * SIXTEEN + (*(p+j) - '0');
    		else if ( *(p+j) >= 'a' && *(p+j) <= 'f' )
    			i = i * j * SIXTEEN + ((*(p+j) - 'a') + 10);
    		j++;
    	}
    	return i;
    }
    It throws exception on *(p+j) = k; statement.

    I am using VC6.0 under NT environment.

    It looks like, when I modify the content of pointer, it throws the exception.

    Please let me know.

    Juganoo


    Code tags added by Hammer

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Where is k defined?
    What not use toupper() or tolower() ? And also, things like isxdigit()
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    38
    Yes K need to be declared. Please assume.


    toupper, isxdigit are standard library routine, they bring lot of unnecessary code. And this will be part of embedded code.
    That is the reason, I want my own routine to do that.

    Thanks,
    Juganoo

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    38
    I am pasting the piece of code here again

    #define SIXTEEN 16

    unsigned short my_atoi(char *p)
    {
    unsigned int i,j;
    int count = 2;

    i= j = 0;

    while( j < count)
    {
    // If you would like to have flexibility to have both upper case
    // and lower case ASCII HEX.
    if ( *(p+j) >= 'A' && *(p+j) <= 'F' )
    {
    int k;
    k = *(p+j)+32;
    *(p+j) = k; ---> it fails here
    printf("Value of p = %d\n",k);
    }

    if ( *(p+j) >= '0' && *(p+j) <= '9' )
    i = i * j * SIXTEEN + (*(p+j) - '0');
    else if ( *(p+j) >= 'a' && *(p+j) <= 'f' )
    i = i * j * SIXTEEN + ((*(p+j) - 'a') + 10);
    j++;
    }
    return i;
    }
    Last edited by Juganoo; 12-04-2002 at 06:49 PM.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Edit your last post, and add code tags to it.

    It will probably fail because you are trying to modify a string literal. I am guessing here, as I can't see how you called the function, but it's the most likely cause.

    Are you doing this kind of thing:
    >printf ("%d\n", my_atoi("FF"));
    If so, it won't work, 'cos the FF string is a string literal that cannot be modified (safely).
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    38
    No, I am invoking like this

    eleID=my_atoi(pCommand+COMM_DTA);

    printf("eleID = %d\n",eleID);

    pCommand is char pointer assigned to global string.

    Is my pointer content modification illegal?

    Thanks,
    Juganoo

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>pCommand is char pointer assigned to global string.
    Can you show me that?

    >>Is my pointer content modification illegal?
    It depends on what it is you're modifying (ie pCommand)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    38
    Here is some more hint.

    135: *(p+j) = k;
    00401403 mov edx,dword ptr [ebp+8]
    00401406 add edx,dword ptr [ebp-8]
    00401409 mov al,byte ptr [k]
    0040140C mov byte ptr [edx],al <---

    It threw exception at last assembly instruction. That means, it did not like content modification. Is there anything like compiler treats pointer content as constant, that is passed on stack?

    That global string declaration is

    char *CommandString4 = "20gM0F[S00C050|S02C023|M03C040|S02C003|S00C006|S03C012|S0 0C050|S02C023|M03C040|S00C050|S02C023|M03C040|S02C 003|S00C006|S03C012|S00C050|S02C023|M03C040|S00C05 0|S02C023|M03C040|E]433";

    It is the 5th and 6th that I am evaluatiing.

    Please let me know.

    Juganoo

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    So, you're trying to modify the data pointed to by CommandString4, yes?

    Well, like I said, it's a string literal, you cannot modify it. You could make it a char array instead:

    >char CommandString4[] = "20gM0F[S00C050.....";

    Either that, or don't modify it at all, instead use a local variable to hold your lower case characters in my_atoi().
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    38
    Thanks a lot.. It did work.

    But I would like to know, what exactly is literal? If it is beyond the scope to explain here. Your pointer somewhere in K and R or Shaum series C book would be enough.

    Like I want to know.

    What is difference between
    char *p="somestring";
    and char p[]="somestring";

    Thanks again.
    Juganoo

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    have a read of this, if you have more questions, ask here again:

    http://www.eskimo.com/~scs/C-faq/q6.2.html
    http://www.eskimo.com/~scs/C-faq/q1.32.html

    Glad we got it going, in the end

    [edit]added more url's
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

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