Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encode string to base32 string in Java

Tags:

java

base32

Just like the title says, I am trying to encode a string "test" into base32 string "ORSXG5A=" in Java.

All I find when searching online is classes that encodes from string to array with 32bits, but obviously that is not what I want.

Sorry for this newbie question.

like image 469
Daniele Testa Avatar asked Feb 02 '14 19:02

Daniele Testa


2 Answers

Apache commons-codec provides a Base32 class that does just that

Base32 base32 = new Base32();
System.out.println(base32.encodeAsString("test".getBytes()));

prints

ORSXG5A=

You can download it here.

like image 63
Sotirios Delimanolis Avatar answered Oct 09 '22 06:10

Sotirios Delimanolis


As @Sotirios Delimanolis wrote it can be done using apache commons but you can also use google guava libraries. For example:

BaseEncoding.base32().encode("test".getBytes());

will return ORSXG5A=.

More information can be found here.

like image 39
pepuch Avatar answered Oct 09 '22 05:10

pepuch