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);
Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    https://stackoverflow.com/questions/11377944/parsing-a-hexadecimal-string-to-an-integer-throws-a-numberformatexception (not exactly a duplicate, but answers contain a solution) – Federico klez Culloca May 02 '20 at 13:42
  • 1
    Does this answer your question? [Converting A String To Hexadecimal In Java](https://stackoverflow.com/questions/923863/converting-a-string-to-hexadecimal-in-java) – Renato May 02 '20 at 13:42
  • @Renato nope, that question is about something else. – Federico klez Culloca May 02 '20 at 13:43
  • Lookup `Integer.parseInt(String s, int radix)` – John3136 May 02 '20 at 13:44
  • 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. – Zabuzard May 02 '20 at 13:46
  • @Zabuzard and ends with this: `String hexString = String.format("%02X%02X%02X", r, g, b);` which gives the format they stated in the question. – Federico klez Culloca May 02 '20 at 13:47
  • 1
    Anyway, OP, since you already have the three components you can just do `int rgb = r << 16 | g << 8 | b` without conversions. – Federico klez Culloca May 02 '20 at 13:49
  • @FedericoklezCulloca I get 16719360 from your solution. –  May 02 '20 at 13:56
  • Are you starting out from the three `int`s `r`, `g` and `b`? Or have you got a string from the outset? Sorry, your question is not quite clear, you should edit it and clarify, please. – Ole V.V. May 02 '20 at 13:58
  • @SirNUB 16719360 is the same value as FF1E00. You’re done. [Try it here](https://www.rapidtables.com/convert/number/decimal-to-hex.html), for example. – Ole V.V. May 02 '20 at 14:01
  • 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. – Federico klez Culloca May 02 '20 at 14:01
  • 1
    Thanks for the edit! There is no such thing as a *hexadecimal `int`*. A Java `int` is represented in binary internally and prints in decimal when you print it. Always. – Ole V.V. May 02 '20 at 14:06

2 Answers2

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();
Community
  • 1
  • 1
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
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
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110