New Life Games LLC

Homebrew Player Tracking and EFT Systems => NLG Homebrew Player Tracking and EFT Systems => Topic started by: peasoup49 on November 04, 2020, 03:06:25 PM

Title: [SOLVED] BCD to Dec/Dec to BCD
Post by: peasoup49 on November 04, 2020, 03:06:25 PM
Anybody have a valid formula that works?  I've tried several but I'm either not applying right, or it doesn't work for what this needs.
Thanks
Code: [Select]
uint8_t BCDtoDEC(uint8_t n)
{
  return (n / 16 * 10 + n % 16);
}


uint8_t DECtoBCD(uint8_t n)
{
  byte b = (n * 103) >> 10;
  return (b * 16 + n - (b * 10));




[UPDATE] It is the right formula.  I was applying it incorrectly.  Also under the DECtoBCD, if you add the line "uint16_t a = n;" before the "byte b" line, it increases the speed of the conversion.
Title: Re: [SOLVED] BCD to Dec/Dec to BCD
Post by: marcdavis on November 07, 2020, 08:58:02 AM
Here is what I am using in my project:



int dec2bcd(byte val)
{
  return ( ((val / 10) << 4) | (val % 10) );
}


int bcd2dec(byte bcd)
{
  return ( ((bcd >> 4) * 10) + (bcd & 0xF) );
}

SimplePortal 2.3.5 © 2008-2012, SimplePortal