简体   繁体   中英

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?

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM