2

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?

Luca Geretti
  • 10,206
  • 7
  • 48
  • 58
  • 1
    The code you've given isn't trying to convert from decimal to hex at all - it's trying to parse hex and then reformat it as hex. It looks like you want the exact same output as input... Note that the very notion of an `int` value being "decimal" or "hex" is meaningless - an `int` is just an integer. It's only when you convert to a textual representation of the number that it matters which base you use. (As an aside, I'd also strongly advise you to follow Java naming conventions.) – Jon Skeet Dec 27 '13 at 12:11
  • yes i am looking for same out put. – jatindersingh Dec 27 '13 at 12:12
  • 1
    Then why perform any conversion at all? Just print out the input... It's really unclear what you're trying to achieve here. – Jon Skeet Dec 27 '13 at 12:12
  • http://stackoverflow.com/questions/13465098/decimal-to-hexadecimal-converter-in-java – Sitansu Dec 27 '13 at 12:12
  • @JonSkeet In a developer's life there are moments where he / she just tries something for educational purposes ("demo java code"). If he / she fails, he is indeed allowed to ask for help ("what i am doing wrong here?"). – Tobias Dec 27 '13 at 12:15
  • @still_learning: But if we knew what the OP was really trying to achieve, we'd be better able to help. I'm hoping the OP will learn a deeper truth about hex and decimal than the question they think they're asking, to be honest. – Jon Skeet Dec 27 '13 at 12:18
  • As a hint to the OP - you should be expecting `RedColor` and `red` to be the same (etc). They aren't. – Jon Skeet Dec 27 '13 at 12:20

3 Answers3

5

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.

akhikhl
  • 2,552
  • 18
  • 23
  • 1
    It would be worth reformatting the code and using normal Java naming conventions (and removing the misleading "decimal" part of the name) just to show what the clearer code would look like... – Jon Skeet Dec 27 '13 at 12:17
1

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
Ronak Jain
  • 2,402
  • 2
  • 24
  • 38
-2

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().

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180