Results 1 to 11 of 11
  1. #1
    nanaji is offline Member
    Join Date
    May 2008
    Posts
    31
    Rep Power
    0

    Default how to convert from BigInteger to Hex

    Hi guys

    I know we can convert from Integer to Hex by using
    int i =2;
    String s = Integer.toHexString(i);

    Like wise

    How can i convert BigInteger bi = new BigInteger("12345678923456");

    How to convert the above "bi" to hexString.

    Thanks
    Regards
    Nanaji

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    Java Code:
            int i =2;
            String s = Integer.toHexString(i);
            System.out.printf("s = %s%n", s);
            BigInteger bi = new BigInteger("12345678923456");
            System.out.printf("bi = %s%n", bi);
            System.out.println("Integer.MAX_VALUE = " + Integer.MAX_VALUE);
            System.out.println("Long.MAX_VALUE = " + Long.MAX_VALUE);
            long n = bi.longValue();
            String hex = Long.toHexString(n);
            System.out.printf("hex = %s%n", hex);

  3. #3
    nanaji is offline Member
    Join Date
    May 2008
    Posts
    31
    Rep Power
    0

    Default

    Hi

    Thanks for the quick reply. I really appreciate. It works fantastic.

    Thanks hardWired.

    Regards
    Nanaji

  4. #4
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    It works like a charm but it is not very safe :). BigInteger.longValue() returns only the lowest 64 bits while BigInteger is meant to represent arbitrary-precision integers. This means you can get a wrong value, not even of the same magnitude and it might be even with the wrong sign (+/-).
    A simpler solution is to use BigInteger.toString(int radix).

    Java Code:
    import java.math.BigInteger;
    
    public class BIHex {
        public static void main(String[] args) {
            java.math.BigInteger bi = new BigInteger(
                    "12345678901234567890123456789012345678901234567890" +
                    "12345678901234567890123456789012345678901234567890");
            System.out.println("max long   = " + Long.toHexString(Long.MAX_VALUE));
            System.out.println("bi as long = " + Long.toHexString(bi.longValue()));
            System.out.println("bi as hex  = " + bi.toString(16));
        }
    }
    When you run this class you see the difference:

    Java Code:
    max long   = 7fffffffffffffff
    bi as long = accff196ce3f0ad2
    bi as hex  = 241fc1742fe8d29593a6afe52b31741cfe5a7f8e67e477381be47851641ef7bf14baccff196ce3f0ad2
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I have thing to clarify. As far as I remember, radix also have a range (a min value and a max value). What happened to the result if the radix is out of rang. I think if the radix out of that range, set to default, to decimal. So...

  6. #6
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    Hi Eranga :)

    Yes you are right. The radix range is 2-36 and it is defined in Character.MIN_RADIX and Character.MAX_RADIX. For a radix out of range the transformation algorithm defaults to radix 10.
    So... what? :) It works fine for radix 16 :)
    I am not sure I got the question. Is there any problem with my example?
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    I got what you say. So I remember that ;) nice to see that.

    What I'm say is, if the radix is 64?? (It can be, right?)

  8. #8
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    The max radix value is 36 because there are only 36 symbols to use for representation (26 letters + 10 digits). If you use 64 will default to 10 so you get the wrong representation. If you want to transform in base 64 you have to write your own algorithm and specify what symbols to use from 37 to 63.
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  10. #10
    nanaji is offline Member
    Join Date
    May 2008
    Posts
    31
    Rep Power
    0

    Default

    Hi ,




    Regards
    Nanaji
    Last edited by nanaji; 05-23-2008 at 04:49 AM.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

Similar Threads

  1. How to convert a string into a BigInteger
    By valery in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 01:32 PM
  2. how to convert String number to int
    By gabriel in forum New To Java
    Replies: 5
    Last Post: 08-02-2009, 03:46 PM
  3. Convert .java to .exe
    By susan in forum New To Java
    Replies: 6
    Last Post: 02-11-2009, 06:47 AM
  4. BigInteger remainder results in zero
    By perito in forum New To Java
    Replies: 1
    Last Post: 03-21-2008, 04:07 PM
  5. how to convert source code to xml
    By valery in forum XML
    Replies: 2
    Last Post: 08-06-2007, 08:29 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •