Does anyone know how to convert decimal to hexadecimal?
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:

http://www.cut-the-knot.com/binary.html

If you do a "View|Page Source" with your browser, you can even access the Javascript code that the author of the page has used.
0
Commented:
Huum, you should read the Web page again!!!
0
Author Commented:
I am not too sure if I understand it, plus they are using Javascript stuff like Math.floor()
0
Commented:

> they are using Javascript stuff like Math.floor()

There is exactly the same in Java!

Class Math: public static native double floor(double a)

0
Author Commented:
I've tried to do everything that he does, and mine does not work correctly.

private String itoHex(int i)
{
final char[] hex_array = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
'C', 'D', 'E', 'F'};

String s = "";

double A = (double)i;

{
double B = A % radix;
s += hex_array[(int)(B+1)];
}

s += hex_array[(int)(A+1)];
return s;
}
0
Commented:
Like this, it's OK!

private String itoHex(int i)
{
final char[] hex_array = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B',
'C', 'D', 'E', 'F'};

String s = "";

double A = (double)i;

{
double B = A % radix;
s = hex_array[(int)(B)] + s;
}

s = hex_array[(int)(A)] + s;
return s;
}
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Author Commented:
Thanks, it works, I was close, but I had the  + 1 happening when I shouldn't have.
0
Commented:
Yes, and also:

s += hex_array[(int)(B)];

is not the same as: s = hex_array[(int)(B)] + s; because you use Strings:

if A = "a" and B = "b":

A += B => "ab"
A =B+A => "ba"
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.