Solved

Converting decimal to hexadecimal...

Posted on 1998-02-03
8
537 Views
Last Modified: 2008-02-01
Does anyone know how to convert decimal to hexadecimal?
0
Comment
Question by:stepma
  • 5
  • 3
8 Comments
 
LVL 5

Expert Comment

by:fontaine
ID: 1232811
I thinck you could be interested by this page:

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

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

Author Comment

by:stepma
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

by:fontaine
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:stepma
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'};
            double radix = 16;
            
            String s = "";

                double A = (double)i;

                while (A >= radix)
                {
                    double B = A % radix;
                    A = Math.floor(A / radix);
                    s += hex_array[(int)(B+1)];
                }
   
               s += hex_array[(int)(A+1)];
            return s;
      }
0
 
LVL 5

Accepted Solution

by:
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'};
                          double radix = 16;

                          String s = "";

                              double A = (double)i;

                              while (A >= radix)
                              {
                              double B = A % radix;
                              A = Math.floor(A / radix);
                              s = hex_array[(int)(B)] + s;
                              }
                             
                              s = hex_array[(int)(A)] + s;
                          return s;
                          }
0
 

Author Comment

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

Expert Comment

by:fontaine
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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.

760 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

26 Experts available now in Live!

Get 1:1 Help Now