If you need to work with Decimal Packed fields, you can use the following 2 functions:
number=UNPACK(packed-number)
after conversion it's a normal integer, which can used for calculations. To reconvert it back, you use:
packed_string=PACK(number,<packed-length>)
I have used it to operate on records (MVS binary files), which contained packed fields. By using the UNPACK/PACK functions I modified the record contents.
The concept of Packed Decimals origins from (IBM) mainframes, I don't know if it's available on other platforms.
Regards
Peter
packed_number="00015D"x
rexno=unpack(packed_number)
SAY 'unpacked number is: 'rexno
rexno=rexno+885
SAY 'ADD '885 'to number, result ='rexno
SAY 'convert it to new packed number'
new_packed_number=pack(rexno)
SAY 'new packed number is: 'unpack(new_packed_number)
EXIT
unpack: PROCEDURE EXPOSE TRACE
PARSE ARG packno
chrstr=C2X(packno)
decno=LEFT(chrstr,LENGTH(chrstr)-1)
SIGN=RIGHT(chrstr,1)
IF VERIFY(SIGN,"ABCDEF" )>0 | ,
VERIFY(decno,"0123456789" )>0 THEN RETURN ""
IF TRANSLATE(SIGN,,"BD")=" " THEN RETURN -decno
RETURN decno
pack: PROCEDURE EXPOSE TRACE
ARG decno,declen
IF decno<0 THEN SIGN='D'
ELSE SIGN='C'
number=SPACE(TRANSLATE(decno,,'+-.')sign,0)
minlen=LENGTH(number)+length(number)//2
IF declen="" THEN declen=minlen
IF minlen>declen THEN RETURN ""
number=X2C(RIGHT(number,declen,'0'))
RETURN number
|