· Apr 9

Convert %Stream.GlobalBinary to Base64

Hi community,

I'm calling to a API that it is retrieving the content of a file as Content of response. I'm catching the binary but I need to convert this Stream to a Base64 string.

I'm trying to convert a %Stream.GlobaBinary to a Base64 string using the following code, but it doesn't work.

do stream1.Rewind()
set response = ""
while 'stream1.AtEnd {
    set temp=stream.Read(4000)
    set temp=$system.Encryption.Base64Encode(temp)
    set response = response_temp

The content is not correctly converted to Base64

Also, I've tried to convert it as dynamic JSon and get the stream as base64

do stream1.Rewind()
set contentfile = {}
set contentfile.file = stream1
set response=contentfile.%Get("file",,"stream>base64")

But the value of response is a %Stream.DynamicBinary

Is there any way to convert the content of the stream as Base64?

I'm sure that it must be very simple, but I don't found it :(

Best regards

Product version: IRIS 2021.1
$ZV: IRIS for Windows (x86-64) 2021.1.3 (Build 389U) Wed Feb 15 2023 14:50:06 EST
Discussion (6)3
Log in or sign up to continue

I think the 4000 character read from the unencoded source is the problem.  That's not an "even number" for base64 encoding, meaning that the encoded version will have trailing "=" signs as padding, in this case 2 of them.

Changing to 3000 characters got it to work for me, meaning that the encoded chunks did not have trailing "=".

An easy test is to look at your final encoded text.  If you see "=" chars anywhere except the end, this is your problem.

The trailing "=" is a problem because when you concatenate the encoded chunks those end up embedded in the result, making it invalid base64.  You need to rig it so you don't get trailing "=" for any chunk except the last one.  You do that by ensuring that your unconverted chunks have a number of bytes such that the bit count is divisible by 6.