I have a task to convert binary coded decimal (BCD) to ascii text. I have to be able to handle 31 digits. An example of decimal 12345 in BCD is X'012345'.
A prototype piece of code I wrote handles up to 7 digits by grabbing each digit, left to right, (shifting and ANDing where needed into a byte) and adds it to a LONG INT accumulator. The next time through the loop, the accumulator is multipled by 10, and the next digit added. Then, I simply used PRINTF to output my value with all needed formatting. Works great for up to the max LONG INT value. (Sometimes I need signed, and sometimes I don't)
However my strategy needs to change to handle up to 31 digits. I suspect the best way to go would be to have a char array to hold the 31 digits and optional sign (and null term char), and work from right to left, so I always end up pointing at the sign position in case I need it. I would grab the last digit, add X'30', and set it into my array at the last position (prior to my null term). Then, back up a byte in the char array, work with the left half of the first input byte, add X'30', put into array, then back up both pointers by one.
(Or, I could initialize my char array to all X'30' and OR the digit in.)
Since the input data is coming in on a file, I don't have to worry about endianness - it's just raw data in a buffer.
The program produces a new data file, with fixed length values that will be used as input into a Load job for an Oracle database.
Thoughts? Performance suggestions? The files I have to process are quite large. The current batch of data is about 750mb.
Todd
A prototype piece of code I wrote handles up to 7 digits by grabbing each digit, left to right, (shifting and ANDing where needed into a byte) and adds it to a LONG INT accumulator. The next time through the loop, the accumulator is multipled by 10, and the next digit added. Then, I simply used PRINTF to output my value with all needed formatting. Works great for up to the max LONG INT value. (Sometimes I need signed, and sometimes I don't)
However my strategy needs to change to handle up to 31 digits. I suspect the best way to go would be to have a char array to hold the 31 digits and optional sign (and null term char), and work from right to left, so I always end up pointing at the sign position in case I need it. I would grab the last digit, add X'30', and set it into my array at the last position (prior to my null term). Then, back up a byte in the char array, work with the left half of the first input byte, add X'30', put into array, then back up both pointers by one.
(Or, I could initialize my char array to all X'30' and OR the digit in.)
Since the input data is coming in on a file, I don't have to worry about endianness - it's just raw data in a buffer.
The program produces a new data file, with fixed length values that will be used as input into a Load job for an Oracle database.
Thoughts? Performance suggestions? The files I have to process are quite large. The current batch of data is about 750mb.
Todd