please dont rip this site

C code for Base (Radix) Conversion

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.

12 bit binary to 3 ASCII HEX digits

//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';

12 bit binary to 4 ASCII Decimal Digits

//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.

16 Bit Binary to 5 ASCII Decimal Digits

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' );
    }

Divide by 10

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 */

Comments:

See also:


file: /Techref/language/ccpp/convertbase.htm, 5KB, , updated: 2009/12/11 13:41, local time: 2017/11/22 01:04,
TOP NEW HELP FIND: 
54.162.241.40:LOG IN

 ©2017 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! / MAKE! / 

<A HREF="http://www.piclist.com/techref/language/ccpp/convertbase.htm"> C code for Base Conversion</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2017 contributors:
o List host: MIT, Site host massmind.org, Top posters @20171122 RussellMc, Van Horn, David, James Cameron, Sean Breheny, alan.b.pearce, IVP, Neil, David C Brown, Bob Blick, Denny Esterline,
* Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.
 

Welcome to www.piclist.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .