在 Java 中将字符串编码为 base32 字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21515479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:05:38  来源:igfitidea点击:

Encode string to base32 string in Java

javabase32

提问by Daniele Testa

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

正如标题所说,我正在尝试将字符串“test”编码为 Java 中的 base32 字符串“ORSXG5A=”。

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

我在网上搜索时发现的只是用 32 位从字符串到数组编码的类,但这显然不是我想要的。

Sorry for this newbie question.

抱歉这个新手问题。

采纳答案by Sotirios Delimanolis

Apache commons-codecprovides a Base32class that does just that

Apache commons-codec提供了一个Base32可以做到这一点的类

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

prints

印刷

ORSXG5A=

You can download it here.

你可以在这里下载。

回答by pepuch

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

正如@Sotirios Delimanolis 所写,它可以使用 apache commons 来完成,但您也可以使用 google guava 库。例如:

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

will return ORSXG5A=.

会回来ORSXG5A=

More information can be found here.

可以在此处找到更多信息。