byte* payload: HEX to DEC conversion

Hi Sifus,

I receive some text via mqtt (PubSubClient.h) which represents a RGB code in (pseudo) HEX format (#FFFFFF = white, #FF0000 = Red, ... you know already).

Issue is I need this pseudo hex info in decimal and split in red,green and blue parts. (#FF0000 => 255,0,0)

I feel I already read the internet twice how to do that. I suspect it's too simple to even write about it? However byte shifting, atoi stoL serial.print(r,HEX) and so on is now entirely confusing me.

I did a bit debugging with Serial.print but I assume it makes it worse since (assumingly) it converts the bytes already into something readable?

void callback(char* topic, byte* payload, unsigned int length) {
  String command((char*)topic);
  String parameter;
  for (int i = 0; i < length; i++) 
    {
    parameter += (char)payload[i];
    }
  if (command.endsWith("/color")){
    Serial.println("Adjust color: ");
    Serial.print(command);
    Serial.print(" - ");
    Serial.println(parameter);
    byte r=(payload>>16);
    byte g=(payload>>8);
    byte b=(payload);
    Serial.print("R: " + r + " G: " + g + " B: " + b);
  }
  else if (command.endsWith("/status")){
    Serial.println("received status: ");
    Serial.print(command);
    Serial.print(" - ");
    Serial.println(parameter);
  }
}

above is one of many attempts. The conversion to String is probably highly unprofessional but the only way I can realize the if clauses. That works well (while it may not be most efficient) but the first block for "/color" does not do the job.
Right now I get invalid operands of types 'byte* {aka unsigned char*}' and 'int' to binary 'operator>>' but this is just one of many different error messages I explored.
Can i not get way with all that scrap and simply use the bytes out of payload to show a pair as DEC format?

Please point me in a good direction. This costs me way too many grey hair already while google always brings me back to those solutions which don't work (for me).

You can use strtoul()

void setup() {
  Serial.begin(115200);

  char colorTxt[] = "FF00FF";
  unsigned long colorInt = strtoul(colorTxt, nullptr, 16);

  byte red = colorInt >> 16;
  byte green = colorInt >> 8;
  byte blue = colorInt;

  printColor("Red", red);
  printColor("Green", green);
  printColor("Blue", blue);
}

void loop() {
  // put your main code here, to run repeatedly:

}

void printColor(char txt[], byte val){
  Serial.print(txt);
  Serial.print(": ");
  Serial.print(val);
  Serial.print(" (0x");
  Serial.print(val, HEX);
  Serial.println(")");
}

should print:

Red: 255 (0xFF)
Green: 0 (0x0)
Blue: 255 (0xFF)

septillion:
should print:

Red: 255 (0xFF)

Green: 0 (0x0)
Blue: 255 (0xFF)

It, indeed, prints exactly.

strtoulong.png

strtoulong.png