In most cases, this is done with printf, but if there are only a few simple needs, including the entire printf function in the program, and it's memory use, can be avoided.
//convert lowest 12 bits binary into hex string with trailing zero
//So if value = 0x02F8 string will contain "2F8\0"
unsigned int value = value to convert;
unsigned char byte;
unsigned char string[4];
for (int i = 0; i < 3; i++) {
var = value & 0x0F;
value <<= 4;
if (var <= 9) // 0..9
string[i] = var + 0x30;
else // A..F
string[i] = var + 0x41 - 10;
}
string[3] = '\0';
//simple, somewhat slow function to convert binary to ASCII decimal
unsigned int value = value to convert;
const unsigned int digits[4] = {1000, 100, 10,1};
unsigned char string[5];
for (int i = 0; i<4; i++) {
string[i] = '0';
while (value >= digits[i]) {value -= digits[i]; string[i]++;}
}
string[4] = '\0';
Note that the comparison (value>=digits[i]) can only be accomplished by subtracting digits[i] from value, which must then be re-calcuated if the comparison is true. Only a very smart C compiler would optimize that by retaining the result of the comparison. The code can be optimized by doing the subtraction first, then breaking out of the while (recode as for) if the result is negative and incrementing string[i] if it is positive. In that case, digits[i] must be added back into the value, or the original for loop can be expanded into 4 seperate loops were the second and forth ADD the digits array value rather than subtract it then test for a positive value to exit the loop, and increment string while the value is still negative.
from: http://www.cs.uiowa.edu/~jones/bcd/decimal.html Very nice tutorial on decimal conversions
Note that all the multiplications in the code can be replaced by shifts and adds (see: Novel Methods of Integer Multiplication and Division ) in order to avoid the use of a math library when coding for a processor without hardware multiply.
void putdec( short int n )
{
unsigned char d4, d3, d2, d1, d0, q;
if (n < 0) {
putchar( '-' );
n = -n;
}
d1 = (n>>4) & 0xF;
d2 = (n>>8) & 0xF;
d3 = (n>>12) & 0xF;
d0 = 6*(d3 + d2 + d1) + (n & 0xF);
q = (d0 * 0xCD) >> 11;
d0 = d0 - 10*q;
d1 = q + 9*d3 + 5*d2 + d1;
q = (d1 * 0xCD) >> 11;
d1 = d1 - 10*q;
d2 = q + 2*d2;
q = (d2 * 0x1A) >> 8;
d2 = d2 - 10*q;
d3 = q + 4*d3;
d4 = (d3 * 0x1A) >> 8;
d3 = d3 - 10*d4;
putchar( d4 + '0' );
putchar( d3 + '0' );
putchar( d2 + '0' );
putchar( d1 + '0' );
putchar( d0 + '0' );
}
On processors without a hardware multiply, base conversion from binary to decimal can be accomplished easily by approximating division by ten.
From: http://www.cs.uiowa.edu/~jones/bcd/divide.html
Code to quickly approximate Q=A/10 to 17 bits (or A < 534,890)
unsigned int A;
unsigned int Q; /* the quotient */
Q = ((A >> 1) + A) >> 1; /* Q = A*0.11 */
Q = ((Q >> 4) + Q) ; /* Q = A*0.110011 */
Q = ((Q >> 8) + Q) >> 3; /* Q = A*0.00011001100110011 */
/* either Q = A/10 or Q+1 = A/10 for all A < 534,890 */
Code to quickly approximate Q=A/10 to 32 bits
unsigned long int A;
unsigned long int Q; /* the quotient */
Q = ((A >> 1) + A) >> 1; /* Q = A*0.11 */
Q = ((Q >> 4) + Q) ; /* Q = A*0.110011 */
Q = ((Q >> 8) + Q) ; /* Q = A*0.11001100110011 */
Q = ((Q >> 16) + Q) >> 3; /* Q = A*0.000110011001100110011001100110011 */
/* either Q = A/10 or Q+1 = A/10 for all 32-bit unsigned A */
| file: /techref/language/ccpp/convertbase.htm, 4KB, , updated: 2008/6/12 10:08, local time: 2008/12/2 13:28,
38.103.63.55:LOG IN
|
| ©2008 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions? Please DO link to this page! Digg it! <A HREF="http://www.sxlist.com/techref/language/ccpp/convertbase.htm"> C code for Base Conversion</A> |
| Did you find what you needed? |
Welcome to sxlist.com!sales, advertizing, & kind contributors just like you! Please don't rip/copy (here's why Copies of the site on CD are available at minimal cost. |
|
.