Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Converte número binário (unsigned) 16-bit no seu equivalente decimal (formato BCD compactado). */
- // Apple Xcode
- //
- // main.c
- //
- // Created by Paulo G.P. on 6/27/11.
- //
- #include <stdio.h>
- void adjust(unsigned char *p)
- {
- unsigned char t = *p + 3;
- if (t & 0x08) *p = t;
- t = *p + 0x30;
- if (t & 0x80) *p = t;
- }
- unsigned long binary2bcd(unsigned int n)
- {
- unsigned char bcd[3] = {0, 0, 0};
- for (int i = 0; i < 16; ++i) {
- adjust(&bcd[0]);
- adjust(&bcd[1]);
- adjust(&bcd[2]);
- bcd[2] <<= 1;
- if (bcd[1] & 0x80) ++bcd[2];
- bcd[1] <<= 1;
- if (bcd[0] & 0x80) ++bcd[1];
- bcd[0] <<= 1;
- if (n & 0x8000) ++bcd[0];
- n <<= 1;
- }
- return (unsigned long)(bcd[2] << 16) | (bcd[1] << 8) | bcd[0];
- }
- int main (int argc, const char * argv[])
- {
- int the_example = 10;
- printf("resultado: %lu\n", binary2bcd(the_example));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement