Convert Binary To Base64

Hey All!

I am been really struggling on this problem and thought it was time to reach out. I need to convert a KNIME Binary Object (an image binary return from an API) to a Base64 string so I can use it in PowerBI to display an image in a dashboard.

I have tried everything I can think of to get the Binary to Base 64 and I have been unsuccessful.

I need to end up with a string output something like this:

/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAFoAWgDASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUE

My data from the GET:
image

My data after Binary To String with ASCII set as the Decode:

I am not very proficient in JAVA and wrote this to try to convert the Binary to a Base64 string:

The JAVA Snippet outputs this:

I tried in Python as well and ended up with a similar result:

import base64
import pandas as pd

input_table_1['body_encoded'] = [base64.b64encode(x.encode('ASCII', 'strict'))
for x in input_table_1.body]

print(input_table_1['body_encoded'])

output_table_1 = input_table_1

The output from my Python Script

It just doesn’t look like the Base64 I’m getting from the online conversion tools. I am open to a JAVA, R or Python solution to this if anyone has any ideas/suggestions. I really appreciate it!

1 Like

Hello @TardisPilot,

maybe this topic helps:

Br,
Ivan

2 Likes

Hi @ipazin,

Thanks for sharing I did not see this! I will use the Palladian node which converts it to the format I need.

1 Like

Hi @TardisPilot , the encoding nodes from Palladian are the best I find. Very user friendly, simple to use.

At one point, I had to find an alternative, as our Knime Server did not have the Palladian extension. I used a Java Snippet, and this is what worked for me:
out_Base64EncodedString = org.apache.commons.codec.binary.Base64.encodeBase64String(c_Content.getBytes());

3 Likes

Hey @bruno29a,

Thanks for sharing! I would definitely like to come up with a native KNIME solution for sure since we can’t always install external extensions.

I ran the JAVA code and it generated this:
image

Whereas the Palladian node generates this (which works perfectly):

I’m not an expert in encoding/decoding data so I wonder what the difference is here and how to get the JAVA to output like the Palladian node.

Hi @TardisPilot , I’m not an expert in this either unfortunately. It looks like the Java code I gave you is giving the same results that you got through Python.

A bit of background on the code I provided, this is what I used convert AWS ClientID+ClientSecret to Base64 when requesting an AWS Cognito token, so it’s basically converting a string to Base64.

I tried different functions in Java back then, but as you are doing too, I converted some string online and via PHP which were giving me the correct values first, and compared with the different trial and error I was doing, until I tried the one I gave you which was giving me the expected values.

I think in your case, you are trying to convert binary to Base64?

Is it possible to share your input data to be encoded so I can play around and see if we can get to the correct solution without the extension?

Edit: And also the expected result of the encoded data?

3 Likes

Hey @bruno29a

I’ll put something together and post it here. Thanks!

1 Like

Hey @bruno29a ,

Here is a workflow I put together to help tackle this:

My ultimate goal is to find a native KNIME method to use (ie JAVA snippet) that generates the same Base64 string that the Base64 Encoder node does.

Thanks again!

1 Like

Hi @TardisPilot , I got it to work!

As I suspected, the code that I gave you was to convert a string to Base64, while you want to convert a binary object to Base64, so it needed some modification.

Taking a step back, the Base64.encodeBase64String() function takes a Byte array (byte[]), and in case of a string, we simply used the .getBytes() method like this stringVariable.getBytes().

However, with the binary object, or more precisely an InputStream, you can’t use this method, but we still need to convert to a Byte array (most of the Base64 methods take a Byte array as argument).

I used the toByteArray() method from the IOUtils libraries (org.apache.commons.io.IOUtils), so you will need to have the “apache commons io” jar files to use it:
https://commons.apache.org/proper/commons-io/download_io.cgi

This is what the Java Snippet looks like in the end:

And you point to the jar file like this:
image

And here is the result of the Base64 encoded (top part from the Base64 Encoder node, bottom part from Java Snippet):

And here’s the workflow: Image_Base64_Test.knwf (760.9 KB)

I added it as a new node:
image

5 Likes

Nice one @bruno29a :+1:
Ivan

1 Like

Hey @bruno29a ,

This is fantastic! I really appreciate you taking the time to document this like you did. I think it’s nice to have a native KNIME solution like this. Thank you again!

2 Likes