如何在java中将BigInteger转换为String
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3028380/
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
How to convert BigInteger to String in java
提问by condinya
I converted a String
to BigInteger
as follows:
我转换一个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);
Now I want my string back. I'm using m.toString()
but that's giving me the desired result.
现在我想要我的绳子回来。我正在使用,m.toString()
但这给了我想要的结果。
Why? Where is the bug and what can I do about it?
为什么?错误在哪里,我该怎么办?
采纳答案by polygenelubricants
You want to use BigInteger.toByteArray()
String msg = "Hello there!";
BigInteger bi = new BigInteger(msg.getBytes());
System.out.println(new String(bi.toByteArray())); // prints "Hello there!"
The way I understand it is that you're doing the following transformations:
我理解的方式是您正在进行以下转换:
String -----------------> byte[] ------------------> BigInteger
String.getBytes() BigInteger(byte[])
And you want the reverse:
而你想要相反的:
BigInteger ------------------------> byte[] ------------------> String
BigInteger.toByteArray() String(byte[])
Note that you probably want to use overloads of String.getBytes()
and String(byte[])
that specifies an explicit encoding, otherwise you may run into encoding issues.
请注意,您可能希望使用String.getBytes()
和 的重载String(byte[])
指定显式编码,否则您可能会遇到编码问题。
回答by krock
Use m.toString()
or String.valueOf(m)
. String.valueOf uses toString() but is null safe.
使用m.toString()
或String.valueOf(m)
。String.valueOf 使用 toString() 但为空安全。
回答by Brian Agnew
Why don't you use the BigInteger(String)
constructor ? That way, round-tripping via toString()
should work fine.
为什么不使用BigInteger(String)
构造函数?这样,往返 viatoString()
应该可以正常工作。
(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)
(另请注意,您转换为字节并没有明确指定字符编码并且是平台相关的——这可能是进一步的悲痛来源)
回答by Nils
http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html
http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html
every object has a toString() method in Java.
Java 中的每个对象都有一个 toString() 方法。
回答by Peter Lawrey
To reverse
扭转
byte[] bytemsg=msg.getBytes();
you can use
您可以使用
String text = new String(bytemsg);
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?
使用 BigInteger 只会使事情复杂化,实际上不清楚为什么要使用 byte[]。打算用 BigInteger 或 byte[] 做什么?重点是什么?
回答by Rayhanur Rahman
String input = "0101";
BigInteger x = new BigInteger ( input , 2 );
String output = x.toString(2);
回答by koZmiZm
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)
使用字符串构造 BigInteger 时,该字符串必须格式化为十进制数。您不能使用字母,除非您在第二个参数中指定基数,否则您最多可以在基数中指定 36。36 只会给你字母数字字符 [0-9,az],所以如果你使用它,你将没有格式。您可以创建: new BigInteger("ihavenospaces", 36) 然后转换回来,使用 .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
但要保持格式:使用几个人提到的 byte[] 方法。这会将格式化的数据打包成最小的大小,并允许您轻松跟踪字节数
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
这对于 RSA 公钥加密系统示例程序来说应该是完美的,假设您保持消息中的字节数小于 PQ 的字节数
(I realize this thread is old)
(我意识到这个线程是旧的)
回答by Withheld
You can also use Java's implicit conversion:
您还可以使用 Java 的隐式转换:
BigInteger m = new BigInteger(bytemsg);
String mStr = "" + m; // mStr now contains string representation of m.
回答by Raj
//How to solve BigDecimal & BigInteger and return a String.
//如何解决BigDecimal & BigInteger并返回一个String。
BigDecimal x = new BigDecimal( a );
BigDecimal y = new BigDecimal( b );
BigDecimal result = BigDecimal.ZERO;
BigDecimal result = x.add(y);
return String.valueOf(result);