1

I am trying to decode a base64 string in SQLRPGLE using SYSTOOLS.BASE64DECODE function. The result is a blank string. The base64 string has CRLF characters as well. When I ommit the CRLF from the base64 string, decode works fine. looks like something to do with the character set. Any help is appreciated

sample base64 string: XlhBCl5GWCoqKioqKipBUEMgTG9nbyoqKioqKioqXkZTCl5GTzU4MCw

SQLRPGLE

Dcl-S VarcharField1 Varchar(4096) ;                          
Dcl-S VarcharField2 varchar(13096) inz(' ') ccsid(819);  
    
VarcharField1 = 'XlhBCl5GWCoqKioqKipBU';                     
                                                             
EXEC SQL SET :VarcharField2 =                                
              SYSTOOLS.BASE64DECODE(:VarcharField1);         
                             
*InLr = *On;       

                                      
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
  • any errors in the joblog after running the code? Those sql procedures run some Java code to do the base64decode. Java stored procedure or user-defined function SYSTOOLS.BASE64DECODE, specific name BASE600002 aborted with an exception "incomplete base64 string: XlhBCl5GWCoqKioqKipBU". – RockBoro Feb 11 '21 at 18:01
  • What's the resulting SQLSTATE? With embedded SQL, that's normally the first place to start. – jtaylor___ Feb 11 '21 at 18:19

1 Answers1

1

It's always a good idea to check SQLSTATE or SQLCODE after running an SQL statement in RPG.

Had you done so you would have seen,
SQLSTATE==38000-->A Java™ routine has exited with an exception.

Looking in the joblog, there's more info

Java stored procedure or user-defined function SYSTOOLS.BASE64DECODE,   
  specific name BASE600002 aborted with an exception "incomplete base64 
  string: XlhBCl5GWCoqKioqKipBU".                                       

As I recall, a base64 encoded string is supposed to be a multiple of 4, and must be padded with '=' if necessary.

However, the string you've given is 21 characters long, which would mean 3 padding characters which isn't valid, you shouldn't ever need more than 2.

If you pad the full string, instead of the substring, it should work.

VarcharField1 = 'XlhBCl5GWCoqKioqKipBUEMgTG9nbyoqKioqKioqXkZTCl5GTzU4MCw=';  

Edit
well it works, in that no exception is thrown. Think there's still a CCSID issue, I don't usually use the SYSTOOLS.BASE64DECODE.

Charles
  • 21,637
  • 1
  • 20
  • 44
  • Thank you. yes, I should have checked the SQL code. Was more focused on the Base64 string. Since I was getting the decoded text without the Cl (CRLF), I did not think there was something wrong with the encoded text itself. And the sample above was just a subset of the whole base64 encoded string. Thanks anyway – siddharth bhandari Feb 11 '21 at 20:12