Jump to content

help with 2 byte hex to 3 byte BCD conversion

Featured Replies

Hi there gents

 

I managed to find this function which converts a 2 byte hex to 2 byte BCD, but since 2 bytes of hex can hold a decimal value 0 to 255, it is limited to a hex value between 0x00 and 0x63 (0 to 99).

 

uint8_t hex2bcd (uint8_t x)
{
uint8_t y;
y = (x / 10) << 4;
y = y | (x % 10);
return (y);
}
I need some help adapting this to produce a 12 bit (can use a 16bit variable) BCD output.
I have developed my own code, but it is very very long and highly inefficient, so I am looking to use the simplest code and/or fasted code possible.
CK

 

edit: came up with this, which doesn't seem to work.

 

uint16_t hex2bcd12bit (uint8_t x)

{

uint16_t B1;
uint16_t B2;
uint16_t B3;
B1 = x & 0x0F;
B2 = (x & 0xF0) >> 4;
B1 = B1 % 10;
B2 = B2 + B1 / 10;
B3 = B2/10;
B2 = B2 % 10;
B3 = B3<<8 + B2<<4 + B1;
return (B3);
}

Edited by CasualKilla

y = (x / 100) << 8; // will get 1xx-2xx


but x contain upper 1/2, and you have to get rid of.


x = x % 100; // modulo


Then the rest of original hex2bcd().


Result type will have to be changed.

Edited by Sensei

Archived

This topic is now archived and is closed to further replies.

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.