Java Hexadecimal to Decimal conversion: Custom Logic

I am trying to figure out how to convert hex into an integer so I can manipulate an RGB light on my arduino. I found a good example of this on the java website, but I'm having a difficult time understanding some of the methods and I am getting hung up. I've added in comments to the code for my kind of thinking through it all.

public class HexToDecimalExample3{    
public static int getDecimal(String hex){    //this is the function which we will call later and they are declaring string hex here. Can we declare string hex inside the scope..?
    String digits = "0123456789ABCDEF";      //declaring string "digits" with all possible inputs in linear order for later indexing
             hex = hex.toUpperCase();        //converting string to uppercase, just "in case"
             int val = 0;                    //declaring int val. I don't get this part.
             for (int i = 0; i < hex.length(); i++)  //hex.length is how long the string is I think, so we don't finish the loop until all letters in string is done. pls validate this
             {  
                 char c = hex.charAt(i);     //char is completely new to me. Are we taking the characters from the string 'hex' and making an indexed array of a sort? It seems similar to indexOf but non-linear? help me understand this..
                 int d = digits.indexOf(c);  //indexing linearly where 0=1 and A=11 and storing to an integer variable
                 val = 16*val + d;           //How do we multiply 16(bits) by val=0 to get a converted value? I do not get this..
             }  
             return val;  
}  
public static void main(String args[]){    
System.out.println("Decimal of a is: "+getDecimal("a"));  //printing the conversions out.
System.out.println("Decimal of f is: "+getDecimal("f"));  
System.out.println("Decimal of 121 is: "+getDecimal("121"));  
}}

I resolved the issue on stack overflow. For those curious about this, the link is here.

Presumably you now want to write this in C++ to run on the Arduino

In C/C++, life seems a million times simpler if you know :wink: Have a look at strtoul

void setup()
{
  // received over serial port
  char text[] = "abcdef";

  // help variable for validation
  char *endptr;
  // result of conversion
  uint32_t rgb;

  Serial.begin(57600);
  Serial.print("received: "); Serial.println(text);
  
  // convert hex text to uinsigned long
  rgb = strtoul(text, &endptr, 16);

  // check
  if (endptr == text)
  {
    Serial.println("No hex digits");
  }
  // other check
  if (*endptr != '\0')
  {
    Serial.print("Invalid character: "); Serial.println(*endptr);
  }

  // result of conversion
  Serial.print("rgb (dec): "); Serial.println(rgb, DEC);
  Serial.print("rgb (hex): "); Serial.println(rgb, HEX);

  Serial.print("r (dec): "); Serial.println((rgb >> 16) & 0xFF);
  Serial.print("r (hex): "); Serial.println((rgb >> 16) & 0xFF, HEX);
  Serial.print("g (dec): "); Serial.println((rgb >> 8) & 0xFF);
  Serial.print("g (hex): "); Serial.println((rgb >> 8) & 0xFF, HEX);
  Serial.print("b (dec): "); Serial.println(rgb & 0xFF);
  Serial.print("b (hex): "); Serial.println(rgb & 0xFF, HEX);
}

void loop()
{

}

Note that this code does not use String (capital S) for the input.

The above code is nearly an exact of the copy of the one on the man page.

// Edit
Added 'extraction' of r, g and b