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 var; 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 */
Note: The major issue with this is accumulated errors. As the size of the number increases, the errors eventually appear in the result.
See this program run with test data at:
https://ideone.com/eziXxN
#include <stdio.h> /* Because lcd and serial don't support printf, and its very costly, and all we need is simple formating with a certain number of digits and precision, this ftoa is enough. */ //avoid needed stdlib.h by defining abs as a macro #define absq(amt) ((amt)<0?0-(amt):(amt)) //avoid needing math.h by defining powers of 10 as an array long pow10[10] = {1,10,100,1000,10000,100000,1000000,10000000,100000000,1000000000}; int puts_int( //send positive integers to STDOUT via putchar(char) unsigned int num //positive integer to display ,char digits //count of digits, negative and '0' pad for trailing zeros ,char pad //character to pad with, or null ) { unsigned int i; char c; unsigned char d; d = absq(digits); while (0 < d) { i = num/pow10[--d]; //pre-increment if (d && !i) { //no digits found yet. d && for leading zero if (pad) putchar(pad); else digits--; //track digits (not) used } else { //found something c = i%10; //only want the lowest digit //Optional: Don't continue after fraction done if ('0'==pad && 0<digits && 0==c) return digits-d; putchar(c+'0'); //convert to ASCII and send } } return absq(digits)-d; } int puts_float( //send floating point numbers to STDOUT float f //number to display , char digits //digits, negative to pad with spaces , char precision //precision, negative to keep trailing zeros ) { unsigned int a; char i=0; if(digits>=10) {return 0;}; // check for negative float if(f<0.0) { //is it negative? putchar('-');i++; //indicate f*=-1; //make it positive if (0>digits) digits++; //optional, steal digit for sign, keep length } a=(int)f; // extract whole number i+=puts_int(a,digits,digits>0?0:' '); if (precision) { putchar('.');i++; f-=(float)a; //remove whole part f*=pow10[absq(precision)]; // promote to precision f+=0.5; // round a=(int)f; // extract whole number i+=puts_int(a,precision,'0'); } return i; //count of characters }
Comments:
12 bit binary to 3 ASCII HEX digits :James Newton of MassMind replies: Thank you! Code updated.+
any faults to remove :
- >>= instead of <<=
- unsigned char var; instead of "unsigned char byte;" as local variable.
See also:
file: /Techref/language/ccpp/convertbase.htm, 8KB, , updated: 2022/5/9 12:09, local time: 2024/7/13 18:17, owner: JMN-EFP-786, |
©2024 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? <A HREF="http://www.massmind.org/techref/language/ccpp/convertbase.htm"> C code for Base Conversion</A> |
Did you find what you needed? |
Welcome to massmind.org! |
Welcome to www.massmind.org! |
.