Search code examples
javabigintegerradix

How do i convert a string that was changed to a different radix in java


I have a string whose radix has been changed & its value is printed. Is there a way to convert the string back to its original form?

My code looks like:

String b=text2[i].toString(16);

Here text2 array is of BigInteger type. I want to get the original string from variable b.


Solution

  • new BigInteger(b, 16).toString();
    

    Sample program:

    public static void main(String[] args) {
        final BigInteger original = new BigInteger("27");
        final String converted = original.toString(16);
        final BigInteger convertedBack = new BigInteger(converted, 16);
    
        System.out.println("original = [" + original.toString() + "]");
        System.out.println("converted = [" + converted + "]");
        System.out.println("convertedBack = [" + convertedBack.toString() + "]");
    }
    

    Output:

    original = [27]
    converted = [1b]
    convertedBack = [27]