Solved

Posted on 1998-02-03
537 Views
Does anyone know how to convert decimal to hexadecimal?
0
Question by:stepma
• 5
• 3

LVL 5

Expert Comment

ID: 1232811

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

LVL 5

Expert Comment

ID: 1232812
Huum, you should read the Web page again!!!
0

Author Comment

ID: 1232813
I am not too sure if I understand it, plus they are using Javascript stuff like Math.floor()
0

LVL 5

Expert Comment

ID: 1232814

> 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 Comment

ID: 1232815
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

LVL 5

Accepted Solution

fontaine earned 40 total points
ID: 1232816
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

Author Comment

ID: 1232817
Thanks, it works, I was close, but I had the  + 1 happening when I shouldn't have.
0

LVL 5

Expert Comment

ID: 1232818
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

## Featured Post

### Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.