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
Actually, there is a faster way than multiplying. However, it relies on good knowledge of the dataset (which is clearly not present in your provided assumptions). For example, if you KNOW that you will convert exactly 2 digits each time, you can convert each digit into a 4-bit value and use a 100 byte long lookup table. This could be repeated for each pair of digits depending on whether they represent 1's, 100's, 10000's, or so on. For example: 1. Get first digit and mask with 0x0F. 2. Get second digit, shift < 4 and mask with 0xF0. 3. Combine. 4. Index into table for value. 5. Add to sum. 6. Repeat. However, this may actually take longer than the x10 multiply. It will certainly take more memory and be more complex. It may even be so complex that undiscovered bugs are easily introduced. Either way, I wouldn't want to work on an application where that kind of thing was done often. Jon