RPGLE convert character to hexadecimal equivalent

Using the C runtime MI functions Convert Character to Hex (CVTHC) provides another method for such conversion in RPGLE. You need to use the C binding directory QC2LE. It converts a each character value in the string to a two-byte value of hex digits.

Prototyping C function cvthc in RPGLE
H DftActGrp(*NO) Option(*SRCSTMT)             
H bnddir('QC2LE')            

D CharToHex       PR                  ExtProc( 'cvthc' )            
D  CTHHex                         *   value                         
D  CTHChar                        *   value                         
D  CTHCharSize                  10I 0 value                         
                                                                    
D charval         S             26a   inz('ATEST123')               
D hexval          S             52a                                 

C                   callp     CharToHex (%addr(hexval)          
C                                       : %addr(charval)        
C                                       : %size(hexval)) 

Complete source Code for Testing. You can put in debug and check.
H DftActGrp(*NO) Option(*SRCSTMT)                                      
H bnddir('QC2LE')                                                      
                                                                       
D HexToChar       PR                  ExtProc('cvtch')                 
D  HTCChar                        *   value                            
D  HTCHex                         *   value                            
D  HTCCharSize                  10I 0 value                            
                                                                       
D CharToHex       PR                  ExtProc( 'cvthc' )               
D  CTHHex                         *   value                            
D  CTHChar                        *   value                            
D  CTHCharSize                  10I 0 value                            
                                                                       
D charval         S             26a   inz('ATEST123')                  
D hexval          S             52a                                    
                                                                       
C                   callp     CharToHex (%addr(hexval)                 
C                                       : %addr(charval)               
C                                       : %size(hexval))               
                                                                       
C     charval       dsply                                              
C     hexval        dsply                                              

                                                                     
C                   callp     HexToChar (%addr(charval)              
C                                       : %addr(hexval)              
C                                       : %size(charval)*2)          
                                                                     
C     charval       dsply                                            
C     hexval        dsply                                            
                                                                     
C                   eval      *inlr = *on         

Recommended Reading

1 comment:

Anonymous said...

Add %Trim to last parameter (length parameter) else error

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.