Here's a little rexx exec I worked up on an IBM mainframe. Assume REXX is pretty similar whatever environment.
Hope it helps. Also included doc from IBM about packed decimal digits. They are quite different.
**********************************************************
/*rexx*/
/* example of displaying COMP numbers from a COBOL program */
/* trace ?r */
comp:
digits=1234
comp.digits=c2x(digits)
display.digits=x2c(comp.digits)
say display.digits
/* example of displaying COMP-3 numbers from a COBOL program */
/*
COMP-3 is packed decimal usually an uneven number, lets use 3
as an example. COBOL would show double that -1, i.e.
77 PACKED-DECIMAL VALUE +0 PIC S9(5) COMP-3.
A packed decimal number will have a sign as the last nibble of
the last byte, i.e., +2345 will, in a hex dump look like
02345C, that's 3 bytes, but can contain up to 5 numbers. If the
number were negative it would be 02345B. B and D are negative, all
other signs are positive.
*/
compthree:
digit=b2x(100011010001011011) /* binary for 02345- */
len=length(digit)
display.digits=left(digit,len-1)
sign=substr(digit,len,1)
sign=translate(sign) /* make it upper case */
if sign='B' or sign='D' then display.digits=display.digits*-1
say display.digits
***************************************************************
Following is from IBM website
Taken from:
Title: ESA/390 Principles of Operation
Document Number: SA22-7201-04
--------------------------------------------------------------------------------
A.1.2 Decimal Integers
Decimal integers consist of one or more decimal digits and a sign. Each
digit and the sign are represented by a 4-bit code. The decimal digits
are in binary-coded decimal (BCD) form, with the values 0-9 encoded as
0000-1001. The sign is usually represented as 1100 (C hex) for plus and
1101 (D hex) for minus. These are the preferred sign codes, which are
generated by the machine for the results of decimal-arithmetic operations.
There are also several alternate sign codes (1010, 1110, and 1111 for
plus; 1011 for minus). The alternate sign codes are accepted by the
machine as valid in source operands but are not generated for results.
Decimal integers may have different lengths, from one to 16 bytes. There are two decimal formats: packed and zoned. In the packed format, each byte contains two decimal digits, except for the rightmost byte, which contains the sign code in the right half. For decimal arithmetic, the number of decimal digits in the packed format can vary from one to 31. Because decimal integers must consist of whole bytes and there must be a sign code on the right, the number of decimal digits is always odd. If an even number of significant digits is desired, a leading zero must be inserted on the left.
In the zoned format, each byte consists of a decimal digit on the right and the zone code 1111 (F hex) on the left, except for the rightmost byte where the sign code replaces the zone code. Thus, a decimal integer in the zoned format can have from one to 16 digits. The zoned format may be used directly for input and output in the extended binary-coded-decimal interchange code (EBCDIC), except that the sign must be separated from the rightmost digit and handled as a separate character. For positive (unsigned) numbers, however, the sign can simply be represented by the zone code of the rightmost digit because the zone code is one of the acceptable alternate codes for plus.
Taken from:
Title: ESA/390 Principles of Operation
Document Number: SA22-7201-04
Build Date: 06/13/97 13:18:22 Build Version: 1.3.0
In either format, negative decimal integers are represented in true notation with a separate sign. As for binary integers, the radix point (decimal point) of decimal integers is considered to be fixed at the right, and any scaling is done by the programmer.
The following are some examples of decimal integers shown in hexadecimal notation:
Decimal
Value Packed Format Zoned Format
+123 12 3C F1 F2 C3
or or
12 3F F1 F2 F3
-4321 04 32 1D F4 F3 F2 D1
+000050 00 00 05 0C F0 F0 F0 F0 F5 C0
or or
00 00 05 0F F0 F0 F0 F0 F5 F0
-7 7D D7
00000 00 00 0C F0 F0 F0 F0 C0
or or
00 00 0F F0 F0 F0 F0 F0
Under some circumstances, a zero with a minus sign (negative zero) is
produced. For example, the multiplicand:
00 12 3D (-123)
times the multiplier:
0C (+0)
generates the product:
00 00 0D (-0)
because the product sign follows the algebraic rule of signs even when the
value is zero. A negative zero, however, is equivalent to a positive zero
in that they compare equal in a decimal comparison.
--------------------------------------------------------------------------------
© Copyright IBM Corp. 1990, 1991, 1993, 1994, 1996, 1997
--------------------------------------------------------------------------------
IBM BookManager® BookServer Copyright 1989, 1999 IBM Corporation. All rights reserved. [sig]<p>PhiloVance<br>Other hobbies, interests: Travel, Model RR (HO Gauge), Genealogy.[/sig]