32

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
condinya
  • 950
  • 4
  • 12
  • 19
  • 3
    If I understand your comments right, your msg is not necessarily a number. I guess most of us believed msg was meant to be the string representing a number (like "12345"). But what you want to do is convert "hello" to some number and be able to reconstruct the original string again. wondering why you'd want to do that... IMO converting string to BigInteger in this way makes only sense if you want to do calculations with the resulting number, but then converting it back wouldn't make sense. What am I missing? – Axel Jun 12 '10 at 11:03
  • actually i m making a program on RSA cryptosystem and in due course i needed to convert msg to BigInteger(during encription) and again the reverse process during decription.but u should answer it without knowing the reason why i m doing because every thing can't be explained here. – condinya Jun 12 '10 at 11:24

9 Answers9

28

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.

polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
9

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

krock
  • 28,904
  • 13
  • 79
  • 85
  • i tried both.If my message is hello then in both ways i get their BigInteger value 448378203247 .but i want hello back not the integer – condinya Jun 12 '10 at 10:55
  • 2
    If the BigInteger does represent a String then there does not seem to be any value in converting it into a byte[] or BigInteger. Why do you need to do that? – krock Jun 12 '10 at 11:24
  • in case of String.valueOf(m) you'll get NullPointerException when m is null – Dmitry Jun 16 '21 at 09:33
8

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)

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • 2
    if i m using like BigInteger(String) constructor i m getting the exception:java.lang.NumberFormatException – condinya Jun 12 '10 at 10:50
  • `BigInteger x = new BigInteger("Hello there");` probably doesn't work. But you can construct a BigInteger from any byte array, even if it was derived from a string. – Rudy Velthuis May 21 '18 at 20:01
7

You can also use Java's implicit conversion:

BigInteger m = new BigInteger(bytemsg); 
String mStr = "" + m;  // mStr now contains string representation of m.
Withheld
  • 4,603
  • 10
  • 45
  • 76
2

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)

koZmiZm
  • 59
  • 4
1

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?

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0
String input = "0101";
BigInteger x = new BigInteger ( input , 2 );
String output = x.toString(2);
Rayhanur Rahman
  • 1,141
  • 2
  • 8
  • 4
0

https://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html.

Every object has a toString() method in Java.

fospathi
  • 537
  • 1
  • 6
  • 7
Nils
  • 13,319
  • 19
  • 86
  • 108
0

//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); 
Raj
  • 11