Base64 UTF-8 Decode in RPGLE almost successful
Posted: Sat Sep 30, 2023 9:12 pm
I'm trying to decode a Base64 string that was Base64 encoded from UTF-8, and it works except that the trailing blanks (x'40') are displaying as '@' signs. I know an '@' sign in UTF-8 is x'40', which is a blank in CCSID 37. I just don't know what to do about it.
For example, "Charlie Brown" encodes to "Q2hhcmxpZSBCcm93bg==", so decoding it should result in "Charlie Brown". It does. However, how do I avoid the x'40'-'@' sign issue?
Here's my code:
/IF DEFINED(*CRTBNDRPG)
h dftactgrp(*no) actgrp(*caller)
/ENDIF
h option(*srcstmt:*nodebugio)
h bnddir('BASE64/BASE64')
/copy base64_h
d regstr2 s a len(1000) varying ccsid(*utf8)
d b64str2 s a len(1000) varying
d retlen s 10i 0
* Max length is 52 for DSPLY
d wrkdsply s 52a
/free
b64str2 = 'Q2hhcmxpZSBCcm93bg==';
%len(regstr2) = %len(regstr2:*max);
retlen = base64_decode(%addr(b64str2:*data):%len(b64str2):
%addr(regstr2:*data):%len(regstr2:*max));
dsply 'Q2hhcmxpZSBCcm93bg== Base64 UTF-8 decoded:';
dsply 'Value S/B Charlie Brown';
eval wrkdsply = regstr2;
dsply wrkdsply;
* Displays "Charlie Brown@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
*inlr = *on;
return;
/end-free
For example, "Charlie Brown" encodes to "Q2hhcmxpZSBCcm93bg==", so decoding it should result in "Charlie Brown". It does. However, how do I avoid the x'40'-'@' sign issue?
Here's my code:
/IF DEFINED(*CRTBNDRPG)
h dftactgrp(*no) actgrp(*caller)
/ENDIF
h option(*srcstmt:*nodebugio)
h bnddir('BASE64/BASE64')
/copy base64_h
d regstr2 s a len(1000) varying ccsid(*utf8)
d b64str2 s a len(1000) varying
d retlen s 10i 0
* Max length is 52 for DSPLY
d wrkdsply s 52a
/free
b64str2 = 'Q2hhcmxpZSBCcm93bg==';
%len(regstr2) = %len(regstr2:*max);
retlen = base64_decode(%addr(b64str2:*data):%len(b64str2):
%addr(regstr2:*data):%len(regstr2:*max));
dsply 'Q2hhcmxpZSBCcm93bg== Base64 UTF-8 decoded:';
dsply 'Value S/B Charlie Brown';
eval wrkdsply = regstr2;
dsply wrkdsply;
* Displays "Charlie Brown@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"
*inlr = *on;
return;
/end-free