Guidance
指路人
g.yi.org
Guidance Forums / Reginald Rexx / Decimal Packed variables

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
Message1. Decimal Packed variables
#13206
Posted by: PeterJ 2010-01-28 18:57:08
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 
 

/* ==============================================================
 * Decimal packed number consists of decimal numbers per 
 * half byte, with the sign in the last half word.
 * The Decimal numbers are from 0-9
 * the sign may be  
 * A,C,E,F for positive number 
 * B,D     for negative number
 * Example: 
 * hex value 01000C represent 1000
 *	         00009D prepresent -9   
 * To convert a decimal packed number towards a rexx compatible 
 * "number" you need to unpack it using the UNPACK function
 * To convert it back, it must be packed via the PACK function  
 * ==============================================================                                      
 */
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 NUMBERs     
 *   UNPACK(packed_number) 
 *      packed_number : is decimal packed number 
 *      return_value  : rexx compatible integer value
 * --------------------------------------------------------------        
 */                                                                     
unpack: PROCEDURE EXPOSE TRACE                                          
PARSE ARG packno                                                        
/* CONVERT PACKED DATA TO HEX AND SPLIT */                              
chrstr=C2X(packno)                                                      
decno=LEFT(chrstr,LENGTH(chrstr)-1)                                     
SIGN=RIGHT(chrstr,1)                                                    
IF VERIFY(SIGN,"ABCDEF" )>0 | ,
   VERIFY(decno,"0123456789" )>0 THEN RETURN ""                                
/* CHECK SIGN */                                                        
IF  TRANSLATE(SIGN,,"BD")=" " THEN RETURN -decno                                 
RETURN decno                                                            
/* -------------------------------------------------------------        
 * PACK DOKID NUMBER    
 *   PACK(number,<pack_length>) 
 *      packed_number : is rexx compatible integer value 
 *      pack_length   : length of returned packed number
 *	                    (defaults to minimum required field) 
 * -------------------------------------------------------------        
 */                                                                     
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                                
Forum List • Thread List • Refresh • New Topic • Search • Previous • Next First 1 Last
掌柜推荐
 
 
 
 
 
 
 
 
 
 
 
 
© Sat 2024-5-4  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0