RPGLE convert hexadecimal to character equivalent

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

Prototyping C function cvthc in RPGLE
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 charval         S              3a                      
D hexval          S              6a   inz('C1C1C1')                  

C                   callp     HexToChar (%addr(charval)                  
C                                       : %addr(hexval)                  
C                                       : %size(charval)*2)                                   

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

No comments:

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.