ascii str (multidigit) or ptr to decimal conversion

Hello!

I'm using following formula to convert string to decimal value:

int ptr2dec(char *ptr, unsigned char size)
{
int val = 0;
unsigned char x;

for (x = 0; x < (size - 1); x++)
{
val += *ptr - 0x30; // get next

// skip last ,multiply step
if (x == size)
return (val);

val *= 10; // shift left
ptr++;
}
}

The idea is to extract digit one by one, and
shift whole value left by multyplying it by 10. Posted code may have errors becos i writing it from my head. My code does not have any error checks (eg: character in string, overflow etc.)

I'm searching for things that can optiomize my code. Can you give me some examples of yours str/dec conversion ? I'm also interested in assembler solution.

sorry for broken English
regards
LB

Parents
  • This is a horribly simple thing you are doing. In fact, the principles apply to any programming language (not just C).

    Here is your original function with formatting added back in. You may want to learn how to do this yourself on discussion forums. When you don't take the time to make it easy for your peers to quickly review your code (even small pieces like this) you insult them and remove all doubts about your own skill and motivation.

    int ptr2dec(char *ptr, unsigned char size)
    {
    int val = 0;
    unsigned char x;
    
    for (x = 0; x < (size - 1); x++)
      {
      val += *ptr - 0x30; // get next
    
      // skip last ,multiply step
      if (x == size)
        return (val);
    
      val *= 10; // shift left
      ptr++;
      }
    }
    

    There are a number of problems and poor choices with your algorithm.

    1. It FAILS if size is 1.
    2. Testing x == size in the loop is a waste of time. It never happens since x < (size - 1) is the loop condition.
    3. Since the loop has a condition, why not use it to test your escape from the loop?
    4. This algorithm does not test for the null-terminator at the end of the string.
    5. This algorithm does not test the character to make sure it's a digit before the conversion.
    6. When the loop exits, your function DOES NOT return val. Is this REALLY what you want to happen?

    Here is my rendetion of your function:

    unsigned int str2dec (
      char *ptr,
      unsigned char size)
    {
    unsigned int val = 0;
    
    for (; size && *ptr; ptr++, size--)
      {
      if (!isdigit(*ptr)) break;
      val = (val * 10) + *ptr - '0';
      }
    
    return (val);
    }
    

    Since this looks like a homework assignment to me, I'm not obliged to explain much more. If you are trying to learn how to do things, you need to study it yourself anyway.

    Jon

Reply
  • This is a horribly simple thing you are doing. In fact, the principles apply to any programming language (not just C).

    Here is your original function with formatting added back in. You may want to learn how to do this yourself on discussion forums. When you don't take the time to make it easy for your peers to quickly review your code (even small pieces like this) you insult them and remove all doubts about your own skill and motivation.

    int ptr2dec(char *ptr, unsigned char size)
    {
    int val = 0;
    unsigned char x;
    
    for (x = 0; x < (size - 1); x++)
      {
      val += *ptr - 0x30; // get next
    
      // skip last ,multiply step
      if (x == size)
        return (val);
    
      val *= 10; // shift left
      ptr++;
      }
    }
    

    There are a number of problems and poor choices with your algorithm.

    1. It FAILS if size is 1.
    2. Testing x == size in the loop is a waste of time. It never happens since x < (size - 1) is the loop condition.
    3. Since the loop has a condition, why not use it to test your escape from the loop?
    4. This algorithm does not test for the null-terminator at the end of the string.
    5. This algorithm does not test the character to make sure it's a digit before the conversion.
    6. When the loop exits, your function DOES NOT return val. Is this REALLY what you want to happen?

    Here is my rendetion of your function:

    unsigned int str2dec (
      char *ptr,
      unsigned char size)
    {
    unsigned int val = 0;
    
    for (; size && *ptr; ptr++, size--)
      {
      if (!isdigit(*ptr)) break;
      val = (val * 10) + *ptr - '0';
      }
    
    return (val);
    }
    

    Since this looks like a homework assignment to me, I'm not obliged to explain much more. If you are trying to learn how to do things, you need to study it yourself anyway.

    Jon

Children
More questions in this forum