Java 如何将 BigInteger 转换为字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21970944/
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 11:45:35  来源:igfitidea点击:

How to convert BigInteger to String?

javastringbytearraybiginteger

提问by SHiv

I decrypted a data and got a BigInt and when I tried to convert it to string I got some Wired characters.

我解密了一个数据并得到了一个 BigInt,当我试图将它转换为字符串时,我得到了一些连线字符。

Code for getting BigInteger

获取 BigInteger 的代码

BigInteger dec = process.Decrypt(sec);

Code for getting ByteArray

获取ByteArray的代码

byte testBy[] = dec.toByteArray();

Code for Converting into String

转换成字符串的代码

String ss = new String(testBy);
System.out.println(ss);

and I tried this code too

我也试过这个代码

String ss = new String(testBy);
System.out.println(ss, "UTF8");

I'm getting this output

我得到这个输出

=^????k+é????3B?+…ò??&??k?U?—c

Help me out here..

在这里帮我..

回答by Don Roby

BigInteger has a toString()method, which gives a decimal string representation of the value.

BigInteger 有一个toString()方法,它给出值的十进制字符串表示形式。

回答by barak manos

A call to System.out.println(dec)should print the decimal representation of deccorrectly.

调用System.out.println(dec)应该dec正确打印十进制表示。

So just make sure that BigInteger dec = process.Decrypt(sec)yields the expected result.

所以只要确保BigInteger dec = process.Decrypt(sec)产生预期的结果。

回答by Devavrata

toString()method convert BigInteger to String.Actually it overrides the Object toString() method.Or directly pass BigInteger object to System.out.println() because println bydefault takes Object.toString().

toString()方法将 BigInteger 转换为 String。实际上它覆盖了 Object toString() 方法。或者直接将 BigInteger 对象传递给 System.out.println() 因为 println 默认采用 Object.toString()。