Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Converting decimal to hexadecimal...

Posted on 1998-02-03
8
Medium Priority
?
580 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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.
Suggested Courses

618 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