Solved

Converting decimal to hexadecimal...

Posted on 1998-02-03
8
568 Views
Last Modified: 2008-02-01
Does anyone know how to convert decimal to hexadecimal?
0
Comment
Question by:stepma
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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:
This video teaches viewers about errors in exception handling.

728 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