Solved

# decimal value in hex

Posted on 2006-05-25
Medium Priority
332 Views
Last Modified: 2010-03-31
how can I convert a decimal number to hexadecimal.
Example 1.98 should be converted to hexadeciaml
0
Question by:wilma22
11 Comments

LVL 86

Expert Comment

ID: 16758484
How do you represent it as hexadecimal (on paper even)?
0

LVL 8

Expert Comment

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

0

LVL 8

Assisted Solution

thomas908 earned 1000 total points
ID: 16758530
found something interesting

http://www.mimosa.org/ftpdown/ieeefp.doc

please have a look
0

Author Comment

ID: 16758537
How to do this in Java?
0

LVL 4

Expert Comment

ID: 16758601
It should be pretty simple.

Please see http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html

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.
0

LVL 86

Expert Comment

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

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

LVL 86

Accepted Solution

CEHJ earned 1000 total points
ID: 16758619
Correction

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

0

LVL 92

Expert Comment

ID: 16758663
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();
0

LVL 14

Expert Comment

ID: 16758688
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
#include<stdio.h>

// 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 {
print_byte(*--b);
} while(b != a);

return 0;
}
0

LVL 8

Expert Comment

ID: 16758742
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...

colr__
0

LVL 86

Expert Comment

ID: 16892211
:-)
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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ā¦
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement āelse ifā and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement ā¦
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
###### Suggested Courses
Course of the Month15 days, 2 hours left to enroll

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

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