1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
//use a table to get a byte sized chunk rather than trying to work in nibbles.
unsigned char bcdtbl[] //use a program to generate this table, I am not THAT into it to
//do this by hand or anything :) this is 0 to 99 in bcd
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
64, 65, 66, 67, 68, 69, 70, 71, 72, 73,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89,
96, 97, 98, 99, 100, 101, 102, 103, 104, 105,
112, 113, 114, 115, 116, 117, 118, 119, 120, 121,
128, 129, 130, 131, 132, 133, 134, 135, 136, 137,
144, 145, 146, 147, 148, 149, 150, 151, 152, 153
};
int main()
{
uintmax_t u {7654};
uintmax_t bcd {};
unsigned char*cp = (unsigned char*)(&bcd);
//whether you go 0-7 or 7 down to 0 for byte order is up to you ...
cp[0] = bcdtbl[u%100]; //first byte is 54 in bcd 4 bit nibbles
cp[1] = bcdtbl[u/100 %100]; //second byte is 76 in bcd nibbles
//... insert more of the above for additional digits.
bitset<64> b = bcd; //unnecessary, used to print output / debug
cout << b << endl;
}
|