how can I convert a packed decimal format (S370Fpd5) in R?

436 Views Asked by At

Can the Packed Decimal Format S370Fpd5 be converted with R or Python? Below are examples with the actual output after ascii conversion, the expected ouptut and also in HEX format.

ACT OUTPUT EXP OUTPUT HEX
....@ 647 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 c0 40
.\177... 703048 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 7f 03 9c f0
..... 859902 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 97 df b0 8c
1

There are 1 best solutions below

2
On

That's not packed decimal data.

It is common for mainframe data to include both text and binary data in a single record, for example a name, a currency amount, and a quantity:

Hopper Grace ar% .

...which would be...

x'C8969797859940404040C799818385404040404081996C004B'

...in hex. This is code page 37, commonly referred to as EBCDIC.

Without knowing that the family name is confined to the first 10 bytes, the given name confined the the subsequent 10 bytes, the currency amount is in packed decimal (also known as binary coded decimal) in the next 3 bytes, and the quantity in the next two bytes, you cannot accurately transfer the data because code page conversion will destroy the currency amount. Converting to code page 1250, commonly in use on Microsoft Windows, you would end up with...

x'486F707065722020202047726163652020202020617225002E'

...where the text data is translated but the packed data is destroyed. The packed data no longer has a valid sign in the last nibble (the lower half of the last byte), the currency amount itself has been changed as has the quantity (from decimal 75 to decimal 11,776 due to both code page conversion and mangling of a big endian number as a little endian number).

In my experience, the best way to avoid these difficulties is to preprocess the file on the mainframe, converting all binary and packed decimal fields to text with embedded explicit signs and decimal points. Then the file can safely go through code page (EBCDIC to ASCII in this case) conversion.

Such preprocessing can easily be done with the mainframe SORT utility, which typically excels at data transformations.

This is from a longer piece I wrote about reading mainframe data on non-mainframe platforms.

There's probably a library to convert the data field-by-field from the source code page to the target code page. For better or worse, requests for recommendations for off-site resources are considered off-topic. You cannot convert an entire file whose records contain packed decimal and/or binary data from one code page to another without at least risking and probably causing data corruption.