Post by IknirI have uploaded a flat file with Packed Decimal String with a length 7.
It does not unpack properly using RPG simple "MOVE" op code. However,
using SQL HEX function converts it but it is slow.
Here is a HEX value of Packed String
x'00000000000123'
SQL HEX function gives a value 123
RPG Move gives 12
Any better soloution please?
Iknir:
Dave gave a perfectly good solution. Overall, the value that you showed
is in a representation known as (I think) BCD or 'binary coded decimal'.
When the digits are stored with the 4-bit rightmost sign, it becomes
'packed BCD' or packed decimal. I'm pretty sure I've seen the term
'packed' used for the storage scheme of your value, but I don't know how
commonly it would be called 'packed' on IBM EBCDIC (Extended Binary
Coded Decimal Interchange Code) systems.
Anyway, you also asked if there was an API. There are APIs available, so
I'll show an example usage in ILE RPG. The sample program has three
parms -- an input and two outputs. The first output is the input broken
out to its hex characters and the second output shows those hex
characters returned to the original byte format. That demonstrates the
two complementary APIs -- cvthc and cvtch.
There is also a test CL program that calls the RPG and dumps the parm
values. You can change the input value to see results. Test various
lengths to see how results can change.
Tom Liotta
----- Begin RPG
* Receives a 16-byte character value as input.
* Converts that to hex digits for output (displayable hex-nibbles).
* Then converts that hex back to char for output.
* Therefore, CharOut should equal CharIn.
H debug
H*option( *NoSrcStmt )
H dftactgrp( *NO )
H actgrp( *CALLER )
H bnddir( 'QC2LE' )
H indent( ' ' )
/eject
*--------------------------------------------------
* Procedure definition
*--------------------------------------------------
D HexChar PR extpgm( 'HEXCHAR' )
D CharIn 16a
Char in
D HexOut 32a
Hex out
D CharOut 16a
Char out
*--------------------------------------------------
* Procedure Interface
*--------------------------------------------------
D HexChar PI
D CharIn 16a
Char in
D HexOut 32a
Hex out
D CharOut 16a
Char out
/eject
D XC_ds Ds align
D dsCharIn 16
D dsHexOut 32
D dsCharOut 16
D CharSize s 10i 0
*
* Convert Hex-to-Character prototype...
* (Convert Hex-nibbles-to-Character-bytes prototype...)
* Length of receiver is double the length of source...
*
D cvthc PR ExtProc( 'cvthc' )
D receiver * Value
D source * Value
D size 10i 0 Value
*
* Convert Character-to-Hex prototype...
* (Convert Character-bytes-to-Hex-nibbles prototype...)
* Length of receiver is half the length of source...
*
D cvtch PR ExtProc( 'cvtch' )
D receiver * Value
D source * Value
D size 10i 0 Value
*
* Convert incoming character to hex...
*
C eval dsCharIn = CharIn
C eval dsHexOut = *blanks
* The size we need is the number of hex-nibbles...
* To include trailing blanks, do not use %trim()...
C eval CharSize = %len( %trim( dsCharIn )) * 2
C Callp cvthc( %Addr( dsHexOut )
c : %Addr( dsCharIn )
c : CharSize
c )
C eval HexOut = dsHexOut
*
* Convert derived hex to outgoing character...
*
* The size we need is the number of bytes of dsHexOut value...
* Note that we simply re-use the previous CharSize...
C Callp cvtch( %Addr( dsCharOut )
c : %Addr( dsHexOut )
c : CharSize
c )
C eval CharOut = dsCharOut
C eval *inLR = *on
C Return
----- End
----- Begin CL
pgm
dcl &CharIn *char 16 value( 'Some string ' )
dcl &HexOut *char 32 value( '***' )
dcl &CharOut *char 16 value( 'zzzzzzzzzzzzzzzz' )
call hexchar ( +
&CharIn +
&HexOut +
&CharOut +
)
dmpclpgm
endpgm
----- End
Tom Liotta
http://zap.to/tl400