java中如何将BigInteger转换为String

发布于 2024-09-05 13:43:56 字数 338 浏览 4 评论 0原文

我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

自演自醉 2024-09-12 13:43:56

您想要使用 BigInteger.toByteArray()

String msg = "Hello there!";
BigInteger bi = new BigInteger(msg.getBytes());
System.out.println(new String(bi.toByteArray())); // prints "Hello there!"

我的理解是,您正在执行以下转换:

  String  -----------------> byte[] ------------------> BigInteger
          String.getBytes()         BigInteger(byte[])

并且您想要相反的转换:

  BigInteger ------------------------> byte[] ------------------> String
             BigInteger.toByteArray()          String(byte[])

请注意,您可能想要使用 String.getBytes( 的重载)String(byte[]) 指定显式编码,否则您可能会遇到编码问题。

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.

你与昨日 2024-09-12 13:43:56

使用m.toString()String.valueOf(m)。 String.valueOf 使用 toString() 但是 null 安全的。

Use m.toString() or String.valueOf(m). String.valueOf uses toString() but is null safe.

咽泪装欢 2024-09-12 13:43:56

为什么不使用 BigInteger(String) 构造函数?这样,通过 toString() 的往返应该可以正常工作。

(另请注意,您对字节的转换没有明确指定字符编码,并且是依赖于平台的 - 这可能是进一步的悲痛根源)

Why don't you use the BigInteger(String) constructor ? That way, round-tripping via toString() 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)

人间不值得 2024-09-12 13:43:56

您还可以使用Java的隐式转换:

BigInteger m = new BigInteger(bytemsg); 
String mStr = "" + m;  // mStr now contains string representation of m.

You can also use Java's implicit conversion:

BigInteger m = new BigInteger(bytemsg); 
String mStr = "" + m;  // mStr now contains string representation of m.
郁金香雨 2024-09-12 13:43:56

使用字符串构造 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)

本王不退位尔等都是臣 2024-09-12 13:43:56

要反转

byte[] bytemsg=msg.getBytes(); 

你可以使用

String text = new String(bytemsg); 

BigInteger 只会让事情变得复杂,事实上不清楚为什么你想要一个 byte[]。计划用 BigInteger 或 byte[] 做什么?重点是什么?

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?

献世佛 2024-09-12 13:43:56
String input = "0101";
BigInteger x = new BigInteger ( input , 2 );
String output = x.toString(2);
String input = "0101";
BigInteger x = new BigInteger ( input , 2 );
String output = x.toString(2);
゛时过境迁 2024-09-12 13:43:56

//如何解决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); 

//How to solve BigDecimal & BigInteger and return a String.

  BigDecimal x = new BigDecimal( a );
  BigDecimal y = new BigDecimal( b ); 
  BigDecimal result = BigDecimal.ZERO;
  BigDecimal result = x.add(y);
  return String.valueOf(result); 
卷耳 2024-09-12 13:43:56

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文