just as titleï¼ now i'am use this method: a=value%1000/100; b=value%100/10; c=value%10; but it's slow,any faster methods? thanksï¼
Don't see any BCD in your question. BCD would store the value 0..9 in four bits, which means that the conversion would be: a = (value >> 8) & 0x0f; b = (value >> 4) & 0x0f; c = value & 0x0f;
In your case, you seem to have a binary value 0..999 stored in a 16-bit integer and want to get out three decimal digits.
The alternative to using div instructions is to do a scan conversion. Basically (a non-optimized program to show the concept):
#include <stdint.h> #include <stdio.h> void convert(uint16_t value) { uint8_t a = 0,b = 0,c = 0; uint8_t a_tmp = 1,b_tmp = 0,c_tmp = 0; uint8_t overflow = 0; int bits_processed = 0; printf("value:%4u -> ",value); while (value) { // Find all non-zero bits in input value //printf("value %u %u %u %u\n",value,c_tmp,b_tmp,a_tmp); if (value & 1) { // We know the value of this bit, so add to our ouput bits. a += a_tmp; b += b_tmp; c += c_tmp; // Catch any decimal overflow to next digit. if (a >= 10) { a -= 10; b++; } if (b >= 10) { b -= 10; c++; } if (c >= 10) { c -= 10; overflow++; } } value >>= 1; if (!value) break; // Next bit to test is worth twice as much. a_tmp <<= 1; b_tmp <<= 1; c_tmp <<= 1; if (a_tmp >= 10) { a_tmp -= 10; b_tmp++; } if (b_tmp >= 10) { b_tmp -= 10; c_tmp++; } if (c_tmp >= 10) { c_tmp -= 10; overflow++; } if (++bits_processed >= 16) { printf("Internal error - hung algorithm\n"); return; } } printf(" %u %u %u",c,b,a); if (overflow) printf(" OVERFLOW!"); printf("\n"); } int main(void) { convert(1); convert(9); convert(11); convert(99); convert(117); convert(999); convert(4692); convert(9999); convert(54321u); convert(65535u); return 0; }
"BCD would store the value 0..9 in four bits"
Some people would call that packed BCD
Yes, it would be packed when you have multiple BCD digits in the same word.
But a non-packed BCD digit would still have the values 0..9 and require four bits.
Now cross-posted at: www.8052.com/.../164580
I do not have malicious,I just want to know that no one have a faster way And willing to share. If I were to do so will lead to unpleasant I will stop this kind of behavior
Hi ou fuqiang,
I come from Taiwan. Nice to meet you.
If you don't mind, I have some suggestions for you.
When you post something in this KEIL forum, please: NOTE: When posting messages, use the following HTML tags to make your message easier to read and understand.
These HTML tags "b", "i", "pre" are described in the above of your message field, where you type your message.
And, when you press the "Preview" button, you shoule preview your post to make sure the format/style/indent is appropriate. This is more important when you post your source code. We need to make our posts be easy to read/understand.
And, if you post your questions twice or more in different forums, people will need to read/answer your questions twice or more, this is wasting people's precious time/resource. So, it is inappropriate.
"And, if you post your questions twice or more in different forums, people will need to read/answer your questions twice or more, this is wasting people's precious time/resource. So, it is inappropriate."
There is nothing wrong in asking the same question on two forums if you cross-link the forums so that users on one forum are aware of the responses people have already given on another forum.
Without the cross-posting links, many people will spend time giving similar answers, and that will waste not only bandwidth but also the time of the people who do spend time helping out.
Since time is a scarce resource, help is an even more valuable and limited resource.
This is my wrong,I have always thought that browse forum is only when they have free time. this is different between china.
View all questions in Keil forum