Dark theme | Light theme

November 4, 2009

Groovy Goodness: Base64 Encoding

The byte[] and String classes in Groovy's GDK have methods to Base64 encode and decode Strings.

def s = 'Argh, Groovy you say, mate?'

String encoded = s.bytes.encodeBase64().toString()
assert 'QXJnaCwgR3Jvb3Z5IHlvdSBzYXksIG1hdGU/' == encoded

byte[] decoded = encoded.decodeBase64()
assert s == new String(decoded)

Run this script on Groovy web console.