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?
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.