xenium:
Packed data is a form of BCD (Binary Coded Decimal). While a more pure form of BCD can 'pack' the value 98 into a single byte, the packed-decimal form would usually require two bytes. This is because the last nibble will contain the sign of the value and therefore move each decimal digit one nibble to the left. The high-order nibble would be set to a leading '0'. The result would be:
08
9F
The first byte is x'09' and the second byte is x'8F'. Together they're x'098F'. I used a x'F' as the sign nibble because there is no indication of positive/negative. The x'F' essentially indicates an absolute value; effectively it's a positive number. More precisely, a x'C' is designated as positive and x'D' is negative.
The value 98 is two digits and takes up two packed bytes. Not particularly an efficient use of the space. But the two packed bytes can hold a range of values from -999 to +999. If 16 bytes were used instead of two bytes, the range of values would be -9(31) through +9(31) where the (31) represents the number of digits. Sixteen bytes is 32 nibbles; minus a single nibble for the sign gives 31 digits. Each digit holds a value from 0 through 9.
Now, if you took those same 16 bytes and used them to store binary integers, you could have a larger range of values; but you run into a problem. There is no precise way to represent decimal _fractions_ in fields formatted as binary integers. Packed-decimal becomes particularly useful when _exact_ representations of decimal fractions is necessary. Most common in business is the representation of fractions of a dollar (cents).
The field definition of packed-decimal then has three general parts: (1) number of total digits, (2) signed/unsigned, and (3) the number of assumed decimal fraction digits. For a monetary field of dollars/cents, the assumed number of fractional digits would usually be '2'.
CPUs that handle packed-decimal have often had basic BCD processing modes. That tends to make them more efficient for business programming where money calculations are prevalent.
Okay, now 'conversion to ASCII'.
Well, usually there is no conversion to ASCII of packed-decimal data. Conversion to ASCII is generally for text data and a packed-decimal field would be considered binary/non-text data. In order to convert to ASCII, you would want to convert the packed data to characters first.
So, assuming a packed-decimal field of three digits and a single assumed decimal position and holding the value x'098D', then the character representation would be [9.8-], i.e. a (negative 9.8)... without the square brackets of course.
An ASCII representation of text [9.8-] is the same as it always is.
In OS/400, one way of handling this conversion more or less automatically is through the use of the CPYTOIMPF (Copy to Import File) command. This command can take a DB2 database file and do the conversions needed to create an ASCII text version. Various command parameters describe the characteristics of the resulting, e.g., whether or not a .CSV format should be created. That version would commonly then be FTPd or otherwise copied to an ASCII system.
Tom
Main Topics
Browse All Topics





by: xeniumPosted on 2006-02-14 at 10:58:50ID: 15953637
Q follows from: http://www.experts-exchang e.com/Prog ramming/ Pr ogramming_ Platforms/ AS400/Q_21 735798.htm l
Note: question is for mainframe packed data, but i think the logic is generic, or is it?
Anyone suggest a better topic area for this?