Originally posted by: BadNewsBears
So I am pretty much binary/hex/ etc retarded. I need to convert regular old ASCII numbers into ANSI/ISO BCD. Is there any easy way?
I'm not immediately familiar with ANSI/ISO standardization for BCD.
ASCII number representation is 0x30 to 0x39 for the digits 0...9 respectively IIRC.
Thus in you could just logical AND the ASCII single byte unsigned character code with 0x0f == 1111B == 15 decimal to mask off the upper 4 bits of the ASCII code thus converting the ASCII BYTE 0x37 into BYTE 0x07 == NIBBLE 7 which is basically BCD in the sense I'm familiar with. i.e. BCD encoding one base-10 digit using 4 bits of storage in binary with 0000 = 0, 0001 = 1, 0010 = 2, 0011 = 3, 0100 = 4, et. al.
Of course you'd then possibly pack the BCD nibbles together into a string of them if you wanted a packed BCD bit string where every group of 4 bits taken individually would encode a decimal digit.
e.g. 011100110101 packed BCD = 735 decimal.
To pack the 4 bit nibbles once you've generated them, just right shift each successively more important BCD 4 bits to the left as you proceed and use logical OR to 'add' the 4 bits into the null string.
e.g. 011100110101 = 000000000000 | 0101 | 00110000 | 011100000000
e.g. 011100110101 = 000000000000 | (0101 << 0) | (0011 << 4) | (0111 << 8)
Of course some library like BOOST or something probably has BCD conversion code for you.. or maybe something in STL or whatever... just depends on your language.