How to convert BigInteger to String in java

前端 未结 9 1645
眼角桃花
眼角桃花 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 13:42

    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)

提交回复
热议问题