Go Down

Topic: BCD conversion (Read 1 time) previous topic - next topic

JohnSan

Looking to convert a 'long to bcd' to drive 5 led displays.
(HP5082 -7302)

Is there a function in a library somewhere to do this, or will it have to be done from scratch.

I know things have moved on since these were about, but would just like to use them....

TIA.
I'll be glad when I've had enough!

mkwired

I'm sure there's a more efficient way to code this.

Code: [Select]

void BCD (unsigned long b, char* o)
{
   for (int i=10; i; --i)
   {
      *o = b % 10;
      b /= 10;
      o++;
   }
}

PeterH


I'm sure there's a more efficient way to code this.

Code: [Select]

void BCD (unsigned long b, char* o)
{
   for (int i=10; i; --i)
   {
      *o = b % 10;
      b /= 10;
      o++;
   }
}



Shouldn't you be packing two digits per byte (one nibble per digit) if you're outputting BCD?
I only provide help via the forum - please do not contact me for private consultancy.

mkwired



I'm sure there's a more efficient way to code this.

Code: [Select]

void BCD (unsigned long b, char* o)
{
   for (int i=10; i; --i)
   {
      *o = b % 10;
      b /= 10;
      o++;
   }
}



Shouldn't you be packing two digits per byte (one nibble per digit) if you're outputting BCD?


You're probably right, but his wasn't clear what he wanted.

GoForSmoke

And probably should use byte (which is unsigned) instead of char (which is signed).
I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Go Up