fast binary to BCD source code

Anyone have C source code to convert 32 bit binary to BCD without using divides. Shift add 3 etc..

Thanks in advance

peejayblack

Reply to
philblack
Loading thread data ...

"philblack":

Something like this? Just rework it for your language, CPU and word-width ;-)

--
Regards,
Arie de Muynck
 Click to see the full signature
Reply to
Arie de Muynck

Here is your homework:

//--------------start of file----------------

extern void bin2bcd(unsigned char *output, unsigned long input);

void bin2bcd(unsigned char *output, unsigned long input) { *output = 0; if (input >= 4000000000) { *output += 0x40; input -= 4000000000; } if (input >= 2000000000) { *output += 0x20; input -= 2000000000; } if (input >= 1000000000) { *output += 0x10; input -= 1000000000; } if (input >= 800000000) { *output += 0x08; input -= 800000000; } if (input >= 400000000) { *output += 0x04; input -= 400000000; } if (input >= 200000000) { *output += 0x02; input -= 200000000; } if (input >= 100000000) { *output += 0x01; input -= 100000000; } output++;

*output = 0; if (input >= 80000000) { *output += 0x80; input -= 80000000; } if (input >= 40000000) { *output += 0x40; input -= 40000000; } if (input >= 20000000) { *output += 0x20; input -= 20000000; } if (input >= 10000000) { *output += 0x10; input -= 10000000; } if (input >= 8000000) { *output += 0x08; input -= 8000000; } if (input >= 4000000) { *output += 0x04; input -= 4000000; } if (input >= 2000000) { *output += 0x02; input -= 2000000; } if (input >= 1000000) { *output += 0x01; input -= 1000000; } output++; *output = 0; if (input >= 800000) { *output += 0x80; input -= 800000; } if (input >= 400000) { *output += 0x40; input -= 400000; } if (input >= 200000) { *output += 0x20; input -= 200000; } if (input >= 100000) { *output += 0x10; input -= 100000; } if (input >= 80000) { *output += 0x08; input -= 80000; } if (input >= 40000) { *output += 0x04; input -= 40000; } if (input >= 20000) { *output += 0x02; input -= 20000; } if (input >= 10000) { *output += 0x01; input -= 10000; } output++; *output = 0; if (input >= 8000) { *output += 0x80; input -= 8000; } if (input >= 4000) { *output += 0x40; input -= 4000; } if (input >= 2000) { *output += 0x20; input -= 2000; } if (input >= 1000) { *output += 0x10; input -= 1000; } if (input >= 800) { *output += 0x08; input -= 800; } if (input >= 400) { *output += 0x04; input -= 400; } if (input >= 200) { *output += 0x02; input -= 200; } if (input >= 100) { *output += 0x01; input -= 100; } output++; *output = 0; if (input >= 80) { *output += 0x80; input -= 80; } if (input >= 40) { *output += 0x40; input -= 40; } if (input >= 20) { *output += 0x20; input -= 20; } if (input >= 10) { *output += 0x10; input -= 10; } *output += (unsigned char)(input & 0x0F); }

unsigned char output[5];

int main(int argc, char* argv[]) {

bin2bcd(output,0xFFFFFFFF); bin2bcd(output,3999999999); bin2bcd(output,0); return 0; }

//--------------end of file----------------

It took me about two hours to think up this method then write and test the code. As you can see there is nothing cleaver or elegant about the code. It's just brute force.

You really should not be so lazy.

Reply to
Keyser Soze

I guess my message did look a bit like a homework request, but really I was looking at why a coworkers code was so slow. I discovered his BCD routine was the culprit with all the divide by 10's. We are displaying a large table of numbers and I know there are better methods to convert.

I was showing him where he ask these types of questions via newsgroups. So this post was for him.

Anyway,

Thanks much for your reply

Phil

Reply to
philblack

Ok, it wasn't homework. :)

The example that Arie de Muynck posted is a very elegant method but it translate poorly to C.

To get a really fast binary to BCD conversion you really need to use assembly language.

The example I posted will generate fast code on a 32 bit CPU, on an 8 bitter it blows chunks.

Reply to
Keyser Soze

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.