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:
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
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!
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());
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?
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).
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!