Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Representing long decimals in hex in Java

Tags:
  1. Jun 14, 2015 #1
    Does anyone know how to represent the following (example) base 10 number as hexadecimal in the Java language?

    2.67367134065437661762356872350435000732582367230003208735000765782365523111637787869816671091367351854359435034651476543984089764356140092091709145675149665165167871324671303963400256123013656501396325676347820
     
  2. jcsd
  3. Jun 14, 2015 #2

    phinds

    User Avatar
    Gold Member
    2016 Award

    I'm not sure I understand the question. It seems like a straightforward algorithm to convert a decimal floating point number to a base 16 floating point number, or is that not what you mean? And it's the algorithm that matters, not what language it is implemented in. Certainly the standard math pack won't do it, if that's what you're asking ...an extended precision algorithm will be required but that's just programming.
     
  4. Jun 14, 2015 #3
    No - it's exactly as you say. I just don't know how to do it. Any specific pointers /code fragments..? (I mention Java as that's my programming skill set.)
     
  5. Jun 14, 2015 #4

    phinds

    User Avatar
    Gold Member
    2016 Award

    Try Planet Source Code. I haven't used it in years but when I used to go there I could almost always find something that helped.
     
  6. Jun 14, 2015 #5

    Filip Larsen

    User Avatar
    Gold Member

    If you don't mind handling the decimal point yourself you can use the BigInteger class [1] which has a constructor that accept a long string of numbers (without decimal point) and a toString method that takes a radix (use 16 for hexadecimal).

    If you like to parse your number with a decimal point but don't need the decimal point on output you can also use the BigDecimal class [2] which can read in decimal numbers with a decimal point and convert the whole number to an unscaled BigInteger which you then can print in hexadecimal. If you really need the decimal point on output too you should be able to extract the integer part and fractional part of that BigDecimal as BigIntegers which you can then print separated by a decimal point.

    [1] https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html
    [2[ https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
     
  7. Jun 15, 2015 #6
    That does not work Filip Larsen - 1.110 ≠ 1.116 (and also ≠ 0.B16).

    You need the apfloat (arbitrary precision floating point) library rather than BigInteger.
     
  8. Jun 16, 2015 #7

    Filip Larsen

    User Avatar
    Gold Member

    Apparently you read some else into the OP's question than I do.

    As a follow-up to this exchange, my comment to use BigInteger/BigDecimal was to point out that there are standard classes for doing it (as long as the OP can manage to handle the decimal point himself). If this is not what the OP wants, then perhaps he should clarify what problem he is trying to solve with the hexadecimal number.
     
  9. Jun 16, 2015 #8

    phinds

    User Avatar
    Gold Member
    2016 Award

    I have to question whether you have really thought this through. Just take the simple decimal number 1.5 If you ignore the decimal point, planning to "take care" of it after the conversion, you have 15 which converts to hex F. Now take the ACTUAL conversion and you go from decimal 1.5 to hex 1.8 How do you "handle the decimal point" to get from F to 1.8 ? Suggesting that this is trivial does not strike me as a helpful approach. Am I missing something?
     
    Last edited: Jun 16, 2015
  10. Jun 16, 2015 #9

    Filip Larsen

    User Avatar
    Gold Member

    Well, I have not been very clear on my assumptions. If you are trying to write up a floating point number in hex then you are right - what I suggest will not work. And the OP does indeed mention "floating point", although it is not clear to me why he wants to express it in hexadecimal.

    Lacking any clear understanding about what the OP wants, I am merely suggesting that if he treat the integer and fractional part as two separate (hex) number strings that just happens to be separated by a decimal point (if he really needs it) then he can use BigInteger. The combined string will not be representing the number as floating point number in the way floating numbers are defined, but since there is no standard way in Java to specify a hex floating point number constant with an arbitrarily number of precision he will have to use a library of some sorts anyway in order to use this number for anything related to arithmetic.

    Granted, without any reason for having the number specified in hex I should probably just have suggested to include the number directly as a BigDecimal with something like
    Code (Java):
    BigDecimal myConstant = new BigDecimal("2.67367134065437661762356872350435000732582367230003208735000765782365523111637787869816671091367351854359435034651476543984089764356140092091709145675149665165167871324671303963400256123013656501396325676347820");
    after which myConstant is available for calculations or convertion using the BigDecimal class.
     
  11. Jun 16, 2015 #10
    The OP states that he wants to represent rational numbers (of which he gives an example) in hex in Java.
    That won't work - BigDecimal does not do number base (radix) conversions: apfloat does which is why I suggested it.
     
  12. Jun 16, 2015 #11

    Filip Larsen

    User Avatar
    Gold Member

    I see no mention of rational numbers by the OP in this thread?

    The code snippet I gave has no need for a radix. It is meant to illustrate that lacking any particular reason for writing the number in hex you may as well include number directly as a decimal number using BigDecimal (or the Apfloat library, if that fits your bill).
     
  13. Jun 16, 2015 #12
    The OP gave an example of a number he wanted to represent: the example is a rational number. As the OP has not returned and his question has been answered I have no more to say on this.
     
  14. Jun 16, 2015 #13
    Side (Silly) Question:-

    Sorry, but this is the second site that's called me the "OP". What does OP mean please? Is it rude :wideeyed: ?
     
  15. Jun 16, 2015 #14
    Original Poster - not rude at all :biggrin:
     
  16. Jun 16, 2015 #15
    You realize that a number with a finite amount of digits in base 10 may have an infinite amount of digits in base 16?
    Anyway, here is an example in Java.
    Code (Text):

    import java.math.BigInteger;
    import java.math.BigDecimal;

    public class Hex {
      public static void main(String[] args) {
        String input = "2.67367134065437661762356872350435000732582367230003208735000765782365523111637787869816671091367351854359435034651476543984089764356140092091709145675149665165167871324671303963400256123013656501396325676347820";
        int maxDigits = 200;
        BigDecimal bd = new BigDecimal(input);
       
        BigInteger bi = bd.toBigInteger();
        String hexString = bi.toString(16);
        bd = bd.subtract(new BigDecimal(bi));
       
        if(!bd.equals(BigInteger.ZERO)) hexString += ".";
       
        while(!bd.equals(BigDecimal.ZERO) && maxDigits-- > 0) {
          bd = bd.multiply(new BigDecimal(16));
          bi = bd.toBigInteger();
          hexString += bi.toString(16);
          bd = bd.subtract(new BigDecimal(bi));
        }
       
        System.out.println(hexString);
      }
    }
     
     
  17. Jun 16, 2015 #16
    Thanks for the code. It's really useful.

    I gave a simple example because I didn't want the thread to drift off in a tangential direction before consensus was reached on an answer.

    I'm trying to convert a billion (10^9) digit file of the decimal expansion of pi into a binary file of 0-255 value bytes. This will give me a file of what I believe to be truly random bits for comparison purposes for some other work I'm undertaking. I can adapt the code for my needs. Just hope that I have enough RAM...
     
  18. Jun 16, 2015 #17
    With a billion digits my code is gonna need a lot of RAM and it will take forever to run.
    But if all you want is random bytes you can just use Random.nextBytes
     
  19. Jun 16, 2015 #18
    Will say a week and 8 GB be sufficient do you think?

    Can't use Random.nextBytes; it's not really. I need undisputedly random.
     
  20. Jun 16, 2015 #19
    PI is not any more random than the output of the Random class. But there is also SecureRandom which produces better random numbers.
     
    Last edited: Jun 16, 2015
  21. Jun 17, 2015 #20
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Representing long decimals in hex in Java
  1. Hex editor (Replies: 16)

  2. MIPS Decimal to Hex (Replies: 3)

Loading...