| |
| |
| |
| Background |
|
History |
| |
| |
|
UH64toBCD is an algorithm to convert a binary number into a BCD (Binary-coded decimal ) representation of the number. While working on a file management subsystem, we encountered the need to display 64 bit file sizes acquired through the WIN32 GetFileSize call on the 32 bit Windows* OS. Needless to say, we found the lack of a function to display 64 bit decimal values curious. Searching the internet was not as fruitful as one would have expected either. So we went ahead and coded our own, which was a simple affair. However, we have posted our algorithm as open source, so that developers in the future would have a reference to work from. |
| |
| |
|
The reference algorithm is provided as a Microsoft* Visual C++ console application. There is no reason that one must only use this development tool to deploy the algorithm. The C language code can be lifted and reused in any platform that can compile C code. In addition, one can modify the code to convert binary numbers that are large or smaller than the 64 bits provided in the example, plus modify the array handler to use native word sizes of 8 or 16 bits, rather than the native 32 bit words that the Intel Pentium platform we used provided. |
| |
| |
|
The Algorithm |
| |
| |
|
The algorithm to convert a binary number to a BCD encoded number is straightforward. The method we use involves accumulating as a BCD number the decimal equivalent of every bit in the binary number. We use arrays to manage the conversion, which allows one to deploy this method on platforms with short word lengths, down to 8 bits. This method will require one array to represent the binary value that will be converted from. Then there are three working arrays that will be used to receive the converted values. One array will contain the BCD representation for every bit of the binary number, and is re-generated for each bit worked on. That array will also need an array of carry bits, so that we can accurately perform the arithmetic without losing any data along the way. The last working array will act as the accumulator of the BCD converted number. |
| |
| |
|
First, one will need to initialize the binary value array. In the example program, we have an array of 64 words. Each word is cleared or set, reflecting the value of the corresponding bit in the number we are converting from. The specific value that a word is set to is inconsequential, as we will only look for zero or non-zero values. If one desired to write tight code, then this initiation step could be avoided, and the individual bits tested. Reason we throw in this initialization step is to bring all bits into a consistent structure for testing values. Otherwise, the testing algorithm will need to react to where the bits may be located as the testing proceeds, instead of running through a straightforward array of words. |
| |
| |
|
Needless to say, the accumulator BCD array (where the final answer will appear) is initialized to 21 words, where each word of which has been cleared to a zero value. |
| |
| |
|
Now we are ready to perform the actual conversion. This is performed through a loop, with the length set to the number of bits we are converting from. In our example, we will be running through a loop of 64 bits. We can run the bit loop with either ascending or descending bit pointers, the same result will occur. The bit loop will run the simple test for the presence of a bit in the number we are converting from. If there is no bit to convert from, then there is nothing to do, and we skip over all activity. If no bits were set, then we would have the expected result of zero in the BCD accumulator array. |
| |
| |
|
The conversion itself will take the presence of a bit to trigger the need to accrue that bit's BCD representation into the BCD accumulator array. So when there is a bit set, then we have some work to do, which is performed in two steps. |
| |
| |
|
First we need to obtain the bit's BCD representation, so we can add it to the BCD accumulator. This is accomplished with the working BCD array, which is also 21 words long in our example. For every bit that we need to add into the BCD accumulator array, we will always first clear out the working BCD array. Then we will preset the first carry bit, which will act as the feed into the bit-doubler algorithm that will generate the BCD value of a particular bit of the number to be converted from. |
| |
| |
|
At this point we can run through a loop that has as many iterations as the position this bit was in the number to be converted from. Starting with the lowest BCD digit, each iteration will take the contents of the current BCD digit of the working BCD array, double its value, and then add in the value of the carry bit. Now the BCD check is performed on the digit. If the contents of the digit is greater than nine, then ten is subtracted from it, the next carry bit is set, and then the pointer to the BCD digit to work on is incremented, otherwise the value is left alone and the carry bit cleared. The loop is completed once we have doubled the working BCD digits for as many times as the bit position. |
| |
| |
|
We now have a BCD number in the working BCD array that we can accrue into the final BCD array. This is performed next in another loop, similar to the one we just left. This time we loop through the 21 digits, adding together a digit from the working BCD array with the accumulator BCD array. Once again the BCD check is performed on this digit. If the contents of the digit are greater than nine, then ten is subtracted from it, the next carry bit is set, otherwise the value is left alone and the carry bit cleared. |
| |
| |
|
Once all 64 bits have been treated this way, we will have accumulated the BCD representation of the 64-bit number. The final step is to use the BCD number in the accumulator array. For an ASCII display, one only needs to add 48 to each BCD digit, and the ASCII value for each number will be ready to display in the array. At this point we also have some code to optionally replace left-leading zeroes with ASCII spaces, for those displays that do not require all digits to be revealed. |
| |
| |
| |
| |
| Downloads |
|
Note: |
| |
| |
|
1- The reference example UH64toBCD program shows how to convert a 64-bit number into a not to exceed a 21-digit BCD number. It was written with Microsoft* Visual Studio C++, but the C code itself could be readily used on any platform. The most important file is named Testu64s.cpp; all other files are intermediate or Microsoft* support files. |
| |
| |
|
Latest files |
| |