This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

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
  • My question is: is there another way to decode chars to decimal without multiply ?

    It is important clearly to define what you trying to achieve.

    Actually, you are converting a string of decimal ASCII characters to binary - not decimal.

    Conversion from one number base to another is generically known as radix conversion. It is common computer science problem and has received a great deal of attention over the years. Although there may be a few tricks possible in special cases, I think it is fair to say that radix conversion generally requires either multiplication or division.

Reply
  • My question is: is there another way to decode chars to decimal without multiply ?

    It is important clearly to define what you trying to achieve.

    Actually, you are converting a string of decimal ASCII characters to binary - not decimal.

    Conversion from one number base to another is generically known as radix conversion. It is common computer science problem and has received a great deal of attention over the years. Although there may be a few tricks possible in special cases, I think it is fair to say that radix conversion generally requires either multiplication or division.

Children
No data