jatindersingh
jatindersingh

Reputation: 69

Java Code issue converting decimal code to hexadecimal?

I have the Java code demo below which is giving me issues.

Here's the example:

public class MyTest 
{
    public static void main(String as[])
    {
        String ColorHex="#4EB3A2";
        int RedColor = Integer.parseInt(ColorHex.substring(1,3), 16);
        int GreenColor = Integer.parseInt(ColorHex.substring(3,5), 16);
        int BlueColor = Integer.parseInt(ColorHex.substring(5,7), 16);
        int finalColorValue = 65536 * RedColor + 256*GreenColor + BlueColor;
        int ColorDecimal=finalColorValue;
        int red = ColorDecimal % 256;
        ColorDecimal = ( ColorDecimal - red ) / 256;
        int green = ColorDecimal % 256;
        ColorDecimal = ( ColorDecimal - green ) / 256;
        int blue = ColorDecimal % 256;
        ColorDecimal = ( ColorDecimal - blue ) / 256;

        String hex = String.format("#%02x%02x%02x", red, green, blue);
        System.out.println("hex"+hex);
    }
}

Here hex should be #4EB3A2 but it is returning #a2b34e. What am I doing wrong here?

Upvotes: 2

Views: 1273

Answers (3)

akhikhl
akhikhl

Reputation: 2578

The following solves your problem:

    String ColorHex="#4EB3A2";

    int RedColor = Integer.parseInt(ColorHex.substring(1,3), 16);
    int GreenColor = Integer.parseInt(ColorHex.substring(3,5), 16);
    int BlueColor = Integer.parseInt(ColorHex.substring(5,7), 16);

    int finalColorValue = 65536 * RedColor + 256*GreenColor + BlueColor;
    int ColorDecimal=finalColorValue;

    // Blue extracted first.
    int blue = ColorDecimal % 256;
    ColorDecimal = (ColorDecimal - blue ) / 256;

    int green = ColorDecimal % 256;
    ColorDecimal = (ColorDecimal - green ) / 256;

    int red = ColorDecimal % 256;
    ColorDecimal = (ColorDecimal - red ) / 256;

    String hex = String.format("#%02x%02x%02x", red, green, blue);
    System.out.println("hex" + hex);

Explanation:

Blue occupies the lowest byte in ColorDecimal, therefore it should be extracted from it first.

Upvotes: 5

Ronak Jain
Ronak Jain

Reputation: 2440

Why you need to write your own code while it can be done easily by

 long parseLong = Long.parseLong("4EB3A2", 16); //hexadecimal to decimal
 String hexString = Long.toHexString(parseLong); //decimal to hexadecimal

Upvotes: 1

Simeon Visser
Simeon Visser

Reputation: 122336

You need to ensure you compute and pass the red, green, blue values in the correct order.

Furthermore, to get the same output, you need to use uppercase X for formatting:

String hex = String.format("#%02X%02X%02X", red, green, blue);

From the documentation, x is the same as X, except that:

Conversions denoted by an upper-case character (i.e. 'B', 'H', 'S', 'C', 'X', 'E', 'G', 'A', and 'T') are the same as those for the corresponding lower-case conversion characters except that the result is converted to upper case according to the rules of the prevailing Locale. The result is equivalent to the following invocation of String.toUpperCase(): out.toUpperCase().

Upvotes: -2

Related Questions