Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal to Hexadecimal Converter in Java

I have a homework assignment where I need to do three-way conversion between decimal, binary and hexadecimal. The function I need help with is converting a decimal into a hexadecimal. I have nearly no understanding of hexadecimal, nonetheless how to convert a decimal into hex. I need a function that takes in an int dec and returns a String hex. Unfortunately I don't have any draft of this function, I'm completely lost. All I have is this.

  public static String decToHex(int dec)
  {
    String hex = "";


    return hex;
  }

Also I can't use those premade functions like Integer.toHexString() or anything, I need to actually make the algorithm or I wouldn't have learned anything.

like image 392
flyingpretzels Avatar asked Nov 20 '12 01:11

flyingpretzels


People also ask

How do you convert decimal to hexadecimal in Java?

To convert decimal to hexadecimal, use any of the two methods i.e. Integer. toHexString() − It returns a string representation of the integer argument as an unsigned integer in base 16. Integer.

How do you write hexadecimal in Java?

In Java code (as in many programming languages), hexadecimal nubmers are written by placing 0x before them. For example, 0x100 means 'the hexadecimal number 100' (=256 in decimal). Decimal values of hexadecimal digits.

Which method converts an int value into its equivalent hexadecimal?

An integer can be converted to a hexadecimal by using the string. ToString() extension method.


3 Answers

One possible solution:

import java.lang.StringBuilder;

class Test {
  private static final int sizeOfIntInHalfBytes = 8;
  private static final int numberOfBitsInAHalfByte = 4;
  private static final int halfByte = 0x0F;
  private static final char[] hexDigits = { 
    '0', '1', '2', '3', '4', '5', '6', '7', 
    '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
  };

  public static String decToHex(int dec) {
    StringBuilder hexBuilder = new StringBuilder(sizeOfIntInHalfBytes);
    hexBuilder.setLength(sizeOfIntInHalfBytes);
    for (int i = sizeOfIntInHalfBytes - 1; i >= 0; --i)
    {
      int j = dec & halfByte;
      hexBuilder.setCharAt(i, hexDigits[j]);
      dec >>= numberOfBitsInAHalfByte;
    }
    return hexBuilder.toString(); 
  }

  public static void main(String[] args) {
     int dec = 305445566;
     String hex = decToHex(dec);
     System.out.println(hex);       
  }
}

Output:

1234BABE

Anyway, there is a library method for this:

String hex = Integer.toHexString(dec);
like image 187
kol Avatar answered Oct 19 '22 19:10

kol


Simple:

  public static String decToHex(int dec)
  {
        return Integer.toHexString(dec);
  }

As mentioned here: Java Convert integer to hex integer

like image 20
Andreas L. Avatar answered Oct 19 '22 20:10

Andreas L.


I need a function that takes in an int dec and returns a String hex.

I found a more elegant solution from http://introcs.cs.princeton.edu/java/31datatype/Hex2Decimal.java.html . I changed a bit from the original ( see the edit )

// precondition:  d is a nonnegative integer
public static String decimal2hex(int d) {
    String digits = "0123456789ABCDEF";
    if (d <= 0) return "0";
    int base = 16;   // flexible to change in any base under 16
    String hex = "";
    while (d > 0) {
        int digit = d % base;              // rightmost digit
        hex = digits.charAt(digit) + hex;  // string concatenation
        d = d / base;
    }
    return hex;
}

Disclaimer: I ask this question in my coding interview. I hope this solution doesn't get too popular :)

Edit June 17 2016 : I added the base variable to give the flexibility to change into any base : binary, octal, base of 7 ...
According to the comments, this solution is the most elegant so I removed the implementation of Integer.toHexString() .

Edit September 4 2015 : I found a more elegant solution http://introcs.cs.princeton.edu/java/31datatype/Hex2Decimal.java.html

like image 13
Raymond Chenon Avatar answered Oct 19 '22 18:10

Raymond Chenon