String to base64 and vice versa

Hi,

I’ve searched through the forum but seen base64 applied only for images. Is it possible convert a string to base64 and decrypt base64 to plain text?

In Javascript I think it’s done with “atob” “btoa”, what about in Processing?

Hi @papaboo

See the link below for a conversion in Java:

But a quick search allowed me getting to the following code from another forum

import javax.xml.bind.DatatypeConverter;
 
void setup()
{
  String b64 = DatatypeConverter.printBase64Binary("Hello World!".getBytes());
  println(b64);
  byte[] origB = DatatypeConverter.parseBase64Binary(b64);
  String orig = new String(origB);
  println(orig);
  exit();
}

Best regards

1 Like

Definitely better to use the Base64 class (as the linked article) from Java 8 onwards - https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html

3 Likes

Hi @neilcsmith

Thanks for the reply. In what sense is it better? Faster, more secure or something else?

I couldn’t find an example in Processing for this. The documentation is very well-written, yes but the Processing syntax is a teensy bit different than Java. ^^

As in a more flexible (multiple formats) and integral part of the Java standard utilities. As opposed to using something that’s part of a utility for doing something else, that might be bringing lots of other things in with it, and has been removed from current JDKs (so will break with Processing 4).

Processing is Java! :wink: OK, it has a couple of annoying teensy syntax differences that cause issues, and aren’t relevant to this.