20

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.

1
  • If you can't find a canned Base32 converter you can find a Base64 one and modify it -- same principles only simpler. Commented Feb 2, 2014 at 20:23

2 Answers 2

37

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Actually the .getBytes() was the only thing I needed :)
@DanieleTesta You might also need to consider the character encoding your String is in.
16

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.