# String to base64 and vice versa

**URL:** <https://discourse.processing.org/t/string-to-base64-and-vice-versa/25690>\
**Category:** Coding Questions\
**Created:** [November 25, 2020, 8:42am UTC](https://discourse.processing.org/t/string-to-base64-and-vice-versa/25690 "2020-11-25T08:42:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![papaboo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/papaboo/32/11236_2.png) [@papaboo](https://discourse.processing.org/u/papaboo)\
**Post date:** [November 25, 2020, 8:42am UTC](https://discourse.processing.org/t/string-to-base64-and-vice-versa/25690/1 "2020-11-25T08:42:24Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![MiguelSanches](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/miguelsanches/32/11172_2.png) [@MiguelSanches](https://discourse.processing.org/u/MiguelSanches)\
**Post date:** [November 25, 2020, 8:50am UTC](https://discourse.processing.org/t/string-to-base64-and-vice-versa/25690/2 "2020-11-25T08:50:41Z")

</div>

Hi @papaboo

See the link below for a conversion in Java:

> **[Java Base64 Encoding and Decoding | Baeldung](https://www.baeldung.com/java-base64-encode-and-decode)**
>
> How to do Base64 encoding and decoding in Java, using the new APIs introduced in Java 8 as well as Apache Commons.

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

```auto
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

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [November 25, 2020, 9:44am UTC](https://discourse.processing.org/t/string-to-base64-and-vice-versa/25690/3 "2020-11-25T09:44:42Z")

</div>

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](https://docs.oracle.com/javase/8/docs/api/java/util/Base64.html)

---

<div class="post-metadata">

**Author:** ![papaboo](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/papaboo/32/11236_2.png) [@papaboo](https://discourse.processing.org/u/papaboo)\
**Post date:** [November 29, 2020, 3:15pm UTC](https://discourse.processing.org/t/string-to-base64-and-vice-versa/25690/4 "2020-11-29T15:15:07Z")

</div>

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. ^^

---

<div class="post-metadata">

**Author:** ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)\
**Post date:** [November 29, 2020, 5:14pm UTC](https://discourse.processing.org/t/string-to-base64-and-vice-versa/25690/5 "2020-11-29T17:14:45Z")

</div>

> [@papaboo](#):
>
> In what sense is it better? Faster, more secure or something else?

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).

> [@papaboo](#):
>
> The documentation is very well-written, yes but the Processing syntax is a teensy bit different than Java.

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