JDubbTX / OpenIBMi

IBM i Open Source Blog
MIT License
0 stars 1 forks source link

https://openyouri.netlify.app/2023/10/12/base-64-encoding-and-decoding-with-db2/ #27

Open utterances-bot opened 3 months ago

utterances-bot commented 3 months ago

Base 64 Encoding and Decoding with DB2 | JDubbTX's Open IBM i Blog

Base64 encoding is a method for converting binary data into ASCII text.

https://openyouri.netlify.app/2023/10/12/base-64-encoding-and-decoding-with-db2/

jkyeung commented 3 months ago

First of all, thank you for this article. It was an interesting read for me, and made me do some digging, which expanded my knowledge and understanding.

I originally wanted to comment on your open question about doing the decoding "in one step". I was going to suggest simply using the same strategy as in IBM's example for encoding: specify a CAST. But I tried doing this in several different ways, and I couldn't get any of them to work. Maybe someone else knows the magic incantation.

I do have something else to mention, though: I think it's incorrect (or misleading at the very least) to refer to the BASE64_DECODE output as "EBCDIC VarBinary" or "binary data in EBCDIC". There is nothing EBCDIC about it. It's just binary, and in your case, since you fed UTF-8 data into BASE64_ENCODE, you get UTF-8 data out from BASE64_DECODE. The only "translating" being done from DecodedTextVarBinary into TranslatedTextVarChar is from CCSID 65535 to CCSID 1208. The raw bytes are actually the same. You can see this for yourself by wrapping results in the HEX function.

And finally, most real-world use cases for Base64 revolve around truly binary data (such as images or PDFs), so the result of BASE64_DECODE would normally be used directly, without translation of any kind.

JDubbTX commented 3 months ago

@jkeung I probably did the same song and dance you did trying to get the decoding "in one step" when I wrote this article.

The one case I have found any use for base64 encoding was when storing private key in a protected variable in my gitlab ci/cd project, but that is bash, not RPG.