decimal value in hex

Posted on 2006-05-25
Last Modified: 2010-03-31
how can I convert a decimal number to hexadecimal.
Example 1.98 should be converted to hexadeciaml
Question by:wilma22
    LVL 86

    Expert Comment

    How do you represent it as hexadecimal (on paper even)?
    LVL 8

    Expert Comment

    Don't think that's possible,
    even windows calulator does not allow it ;)

    LVL 8

    Assisted Solution

    found something interesting

    please have a look

    Author Comment

    How to do this in Java?
    LVL 4

    Expert Comment

    It should be pretty simple.

    Please see

    Do "View - Source" on the page and you should see great chunks of Java Script code that does what you need.  The logic looks pretty simple to convert into Java.  The licence seems quite permissive, so I don't see a problem with using it.
    LVL 86

    Expert Comment

    If you're talking about the internal representation, you might try

    String hex = Long.toString(doubleToRawLongBits(1.98), 2);
    LVL 86

    Accepted Solution


    String hex = Long.toString(Double.doubleToRawLongBits(1.98), 16);

    LVL 92

    Expert Comment

    you could split it and convert each half

    String s = "1.98";
    String[] tokens = s.split("\\.");
    String hex = Integer.parseInt(tokens[0]).toHexString()+"."+Integer.parseInt(tokens[1]).toHexString();
    LVL 14

    Expert Comment

    CEHJ's comment is correct to show the internal representation
    here is the C version that can be used to show binary representation of any type
    you can use it thru JNI

    // displays binary form of any variable

    // prints 8 bits of a character
    void print_byte(char c) {
          unsigned char t;
          for(t = 0x80; t != 0; t >>= 1)
                putchar(c & t ? '1' : '0');

    int main() {
          double d = 1.98; // any type is acceptable
          char *a = (char*) &d, *b = a + sizeof(d);
          printf("%f\n", d);
          do {
          } while(b != a);
          return 0;
    LVL 8

    Expert Comment

    There is not concept of a decimal place in hexidecimal, at least not the same as with decimal.

    If you dont get it working from the above, try converting the decimal to binary, then convert the binary (which shoudld be in 2's complement) to hex. This should then be able to convert back to the decimal value based on a 2's complement conversion algorithm.

    Correct me if Im wrong experts...

    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Suggested Solutions

    Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
    Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
    Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now