java中如何将BigInteger转换为String
我将 String
转换为 BigInteger
,如下所示:
Scanner sc=new Scanner(System.in);
System.out.println("enter the message");
String msg=sc.next();
byte[] bytemsg=msg.getBytes();
BigInteger m=new BigInteger(bytemsg);
现在我想要回我的字符串。我正在使用 m.toString() 但这给了我想要的结果。
为什么?该错误在哪里?我可以采取什么措施?
I converted a String
to BigInteger
as follows:
Scanner sc=new Scanner(System.in);
System.out.println("enter the message");
String msg=sc.next();
byte[] bytemsg=msg.getBytes();
BigInteger m=new BigInteger(bytemsg);
Now I want my string back. I'm using m.toString()
but that's giving me the desired result.
Why? Where is the bug and what can I do about it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您想要使用
BigInteger.toByteArray()
我的理解是,您正在执行以下转换:
并且您想要相反的转换:
请注意,您可能想要使用
String.getBytes( 的重载)
和String(byte[])
指定显式编码,否则您可能会遇到编码问题。You want to use
BigInteger.toByteArray()
The way I understand it is that you're doing the following transformations:
And you want the reverse:
Note that you probably want to use overloads of
String.getBytes()
andString(byte[])
that specifies an explicit encoding, otherwise you may run into encoding issues.使用
m.toString()
或String.valueOf(m)
。 String.valueOf 使用 toString() 但是 null 安全的。Use
m.toString()
orString.valueOf(m)
. String.valueOf uses toString() but is null safe.为什么不使用
BigInteger(String)
构造函数?这样,通过 toString() 的往返应该可以正常工作。(另请注意,您对字节的转换没有明确指定字符编码,并且是依赖于平台的 - 这可能是进一步的悲痛根源)
Why don't you use the
BigInteger(String)
constructor ? That way, round-tripping viatoString()
should work fine.(note also that your conversion to bytes doesn't explicitly specify a character-encoding and is platform-dependent - that could be source of grief further down the line)
您还可以使用Java的隐式转换:
You can also use Java's implicit conversion:
使用字符串构造 BigInteger 时,该字符串必须格式化为十进制数。不能使用字母,除非在第二个参数中指定基数,基数最多可以指定 36。 36 只会给你字母数字字符 [0-9,az],所以如果你使用它,你将没有格式。您可以创建: new BigInteger("ihavenospaces", 36)
然后要转换回来,请使用 .toString(36)
但要保持格式:
使用一些人提到的 byte[] 方法。这会将数据打包成最小的格式,并允许您轻松跟踪字节数,
这对于 RSA 公钥加密系统示例程序来说应该是完美的,假设您将消息中的字节数保持为小于PQ 的字节数
(我意识到这个线程很旧)
When constructing a BigInteger with a string, the string must be formatted as a decimal number. You cannot use letters, unless you specify a radix in the second argument, you can specify up to 36 in the radix. 36 will give you alphanumeric characters only [0-9,a-z], so if you use this, you will have no formatting. You can create: new BigInteger("ihavenospaces", 36)
Then to convert back, use a .toString(36)
BUT TO KEEP FORMATTING:
Use the byte[] method that a couple people mentioned. That will pack the data with formatting into the smallest size, and allow you to keep track of number of bytes easily
That should be perfect for an RSA public key crypto system example program, assuming you keep the number of bytes in the message smaller than the number of bytes of PQ
(I realize this thread is old)
要反转
你可以使用
BigInteger 只会让事情变得复杂,事实上不清楚为什么你想要一个 byte[]。计划用 BigInteger 或 byte[] 做什么?重点是什么?
To reverse
you can use
using a BigInteger just complicates things, in fact it not clear why you want a byte[]. What are planing to do with the BigInteger or byte[]? What is the point?
//如何解决BigDecimal & BigInteger 并返回一个 String。
//How to solve BigDecimal & BigInteger and return a String.
https://docs.oracle.com/ javase/1.5.0/docs/api/java/lang/Object.html。
Java 中的每个对象都有一个 toString() 方法。
https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html.
Every object has a toString() method in Java.