Solved

Converting decimal to hexadecimal...

Posted on 1998-02-03
8
553 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 32
Configure a Bean in an XML file 4 42
maven module vs maven project 3 46
going to wrong jsp page 2 33
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

856 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