ANSI/ISO BCD encoding

BadNewsBears

Diamond Member
Dec 14, 2000
3,426
0
0
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?
 

QuixoticOne

Golden Member
Nov 4, 2005
1,855
0
0
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.

 

QuixoticOne

Golden Member
Nov 4, 2005
1,855
0
0
Ok, ok i meant LEFT shift. YES, I can tell my right from my left.... sometimes.

Since you say you're bin/hex challenged, just to make it fully explicit:
0 = 0x0 = 0000 = 0x30 ASCII
1 = 0x1 = 0001 = 0x31 ASCII
2 = 0x2 = 0010 = 0x32 ASCII
3 = 0x3 = 0011 = 0x33 ASCII
4 = 0x4 = 0100 = 0x34 ASCII
5 = 0x5 = 0101 = 0x35 ASCII
6 = 0x6 = 0110 = 0x36 ASCII
7 = 0x7 = 0111 = 0x37 ASCII
8 = 0x8 = 1000 = 0x38 ASCII
9 = 0x9 = 1001 = 0x39 ASCII
A = 0xA = 1010 = 0x41 ASCII
B = 0xB = 1011 = 0x42 ASCII
C = 0xC = 1100 = 0x43 ASCII
D = 0xD = 1101 = 0x44 ASCII
E = 0xE = 1110 = 0x45 ASCII
F = 0xF = 1111 = 0x46 ASCII.

So one decimal or hex digit fits in 4 bits, with several of the possible 4-bit codes not needed for representing decimal digits, but all of them being fully used to represent one full hex digit.

Converting from ASCII decimal digits to and from BCD is trivial, though going from ASCII HEX back and forth to 4-bit binary is a little more complex because the pattern of the ASCII codes changes between 0-9 and A-F (uppercase), and of course (a-f) lowercase are completely different codes.

 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |