How to convert BigInteger to String in java

前端 未结 9 1602
眼角桃花
眼角桃花 2020-12-24 13:39

I converted a String to BigInteger as follows:

Scanner sc=new Scanner(System.in);
System.out.println(\"enter the message\");
String         


        
相关标签:
9条回答
  • 2020-12-24 14:02

    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?

    0 讨论(0)
  • 2020-12-24 14:06

    You can also use Java's implicit conversion:

    BigInteger m = new BigInteger(bytemsg); 
    String mStr = "" + m;  // mStr now contains string representation of m.
    
    0 讨论(0)
  • 2020-12-24 14:07

    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)

    0 讨论(0)
提交回复
热议问题