0

I have 3 RGB values and I want to get the hexadecimal int value that represents them, combined.

For example, suppose I have R=255, G=30, B=0, this is FF1E00, how to get the equivalent int value?

So far I have managed to get a hexadecimal string FF1E00 from the values, but how to get the int value now?

// User defined rgb value
int r = 255;
int g = 30;
int b = 0;
// Converts rgb to hex, but in string
String hexString = String.format("%02X%02X%02X", r, g, b);
12
  • 1
    stackoverflow.com/questions/11377944/… (not exactly a duplicate, but answers contain a solution) Commented May 2, 2020 at 13:42
  • 1
    Does this answer your question? Converting A String To Hexadecimal In Java Commented May 2, 2020 at 13:42
  • 1
    Your text description does not line up with the code example. You say you have a string input in hexadecimal already (but uppercase and without 0x prefix). But your code starts with 3 RGB integer values. Voting to close because needs clarity. Commented May 2, 2020 at 13:46
  • 1
    Anyway, OP, since you already have the three components you can just do int rgb = r << 16 | g << 8 | b without conversions. Commented May 2, 2020 at 13:49
  • 1
    @SirNUB of course you do. A number is a number, it's not a hex number or a decimal number. A representation only makes sense in the context of a string. Commented May 2, 2020 at 14:01

2 Answers 2

1

RGB to int (after OP clarified)

You said you would like to convert your three RGB values to an integer. The integer ff1e00 (hexadecimal) which is 16719360 in decimal.

To do so, you can go directly from the RGB values using the following logic:

int hex = r << 16 | g << 8 | b;

Alternativly you can also convert your hexString back to an integer:

int hex = Integer.parseInt(hexString, 16);

By the way, do not expext System.out.println(hex) to print a hexadecimal value. It will be 16719360, so decimal. int does not store any information about its base. Integers are unaware of a base. They only come into play once you want to visualize them, i.e. go to a String. And then you can just use the formatter, as you already figured out.


RGB to hex string (before OP clarified)

Your are very close. You already managed to convert your RGB values to a hexadecimal string FF1E00. To get 0xff1e00 you simply have to add 0x in front of it and make everything lowercase. Fortunately, both can be done compact with the formatter directly:

String.format("0x%02x%02x%02x", r, g, b);

I added 0x in front of the format and changed your %...X template to %...x which gives lowercase-hexadecimal integers.


Alterntively you can also do easy string manipulation afterwards:

hexString = "0x" + hexStrong.toLowerCase();
Sign up to request clarification or add additional context in comments.

4 Comments

But that gives a string, not exactly what I need, which is similar to: int hexValue = 0xff1e00
What do you need then? Please clarify.
@SirNUB So you want 0xff1e00 as int value? I.e. 16719360?
@SirNUB I updated my answer based on your comment, is it now correct?
0

You can do it as follows:

public class Main {
    public static void main(String[] args) {
        int r = 255;
        int g = 30;
        int b = 0;
        String hexString = String.format("%02X%02X%02X", r, g, b);
        int hex = Integer.parseInt(hexString, 16);
        System.out.println(hex);
        String hexString1 = Integer.toHexString(hex);
        System.out.println(hexString1);
        String hexString2 = String.format("0x%02x%02x%02x", r, g, b);
        System.out.println(hexString2);
    }
}

Output:

16719360
ff1e00
0xff1e00

4 Comments

Note that the format can do it already without toLowerCase(), just %...x instead of %...X.
I don't want a hexadecimal string, i need a value.
@Zabuzard - Thank you. I've incorporated the same.
@SirNUB - Check the updated answer.