?
Solved

Converting decimal to hexadecimal...

Posted on 1998-02-03
8
Medium Priority
?
587 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 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