Having an issue converting HEX string to a usable int

I am trying to make an RGB LED controller that uses Python to send hex data over the serial port to my Arduino which will then convert the hex data to an int (0-255) that can then be applied to the LED's. However, in the code I have pasted below, I don't think my hex to int conversion is working correctly because when I input 255 into python, It gets converted to FF as I'd expect it to and is sent to the Arduino. The Arduino then takes FF and converts it to 15, that is not what should be happening. Is there something I am missing? Or am I using a function incorrectly? I've been stuck on this for a few hours now and am still somewhat of a beginner so any and all help is greatly appreciated!

#define RED 6
#define GREEN 3
#define BLUE 5

void setup()
{
  Serial.begin (9600);
  pinMode (RED, OUTPUT);
  pinMode (GREEN, OUTPUT);
  pinMode (BLUE, OUTPUT);
}

void loop()
{
  int val;
  int result;
  String inHex;
  char hexVal[7];
  char temp[2];
  int red;

  while (Serial.available() == 0);
  while (Serial.available() > 0)
  {
    inHex = Serial.readString();
    Serial.println(inHex);
    
    inHex.toCharArray(temp, 2);
    
    red = strtol(temp, 0, 16);
    
    analogWrite(RED, red);
    Serial.print("RED VALUE IS: ");
    Serial.println(String(red));  
  }  
}

Why are you fooling around with strings when you're interested in bytes of data? You should be sending and recieving bytes. No conversion necessary.

If you for some reason want to stick with sending hex values it is not that hard to do the conversion yourself:

const int noValues = 3;
byte value[noValues];
const byte RED = 6;
const byte GREEN = 3;
const byte BLUE = 5;
byte ledPin[noValues] = {RED, GREEN, BLUE};

void setup(){
    Serial.begin(115200);
    for(int i = 0; i<noValues; i++) pinMode(ledPin[i], OUTPUT);
}//setup()

void loop(){
  while(!Serial.available());//wait for char
  for(int i = 0; i<noValues; i++){//read all values
    while(!Serial.available());//wait for char
    value[i] = hexDigit(Serial.read())<<4;
    while(!Serial.available());//wait for char
    value[i] += hexDigit(Serial.read());
  }//for(i)
  while(!Serial.available());//wait for char
  Serial.read();//read past nl
  while(!Serial.available());//wait for char
  Serial.read();//read past cr
  for(int i = 0; i<noValues; i++) analogWrite(ledPin[i], value[i]);
}//loop()

char hexDigit(char c){
  if(c>='0'&&c<='9') return c-'0';
  if(c>='a'&&c<='f') return c-'a'+10;
  if(c>='A'&&c<='F') return c-'A'+10;
  return -1;//error
}//hexDigit()

KenF:
Why are you fooling around with strings when you’re interested in bytes of data? You should be sending and recieving bytes. No conversion necessary.

Because I am using the library PySerial which can only send strings across the serial port. It mad the most sense to convert my RGB values to hex so I can have a consistent 6 character string where the first/second values correspond to red, third/fourth to green and fifth/sixth to blue. In the current state I’m only dealing with red so I can get the conversion working correctly and then I’ll incorporate the other colors.

nilton61:
If you for some reason want to stick with sending hex values it is not that hard to do the conversion yourself:

const int noValues = 3;

byte value[noValues];
const byte RED = 6;
const byte GREEN = 3;
const byte BLUE = 5;
byte ledPin[noValues] = {RED, GREEN, BLUE};

void setup(){
    Serial.begin(115200);
    for(int i = 0; i<noValues; i++) pinMode(ledPin[i], OUTPUT);
}//setup()

void loop(){
  while(!Serial.available());//wait for char
  for(int i = 0; i<noValues; i++){//read all values
    while(!Serial.available());//wait for char
    value[i] = hexDigit(Serial.read())<<4;
    while(!Serial.available());//wait for char
    value[i] += hexDigit(Serial.read());
  }//for(i)
  while(!Serial.available());//wait for char
  Serial.read();//read past nl
  while(!Serial.available());//wait for char
  Serial.read();//read past cr
  for(int i = 0; i<noValues; i++) analogWrite(ledPin[i], value[i]);
}//loop()

char hexDigit(char c){
  if(c>=‘0’&&c<=‘9’) return c-‘0’;
  if(c>=‘a’&&c<=‘f’) return c-‘a’+10;
  if(c>=‘A’&&c<=‘F’) return c-‘A’+10;
  return -1;//error
}//hexDigit()

I’ll have to try this when I get home from class.

I knew there were manual ways of converting Hex but I figured it’d be cleaner to just use the strtol() function. Does this function not have the ability to convert hex? What is the primary use of this function if not for what I assumed? I only ask so that I know for future reference.

strtol() can convert strings of any base into binaries. But since it is that general it can be a bit clumsy. And the conversion is, as i demonstrated, quite simple. So its a balance between using general lib functions which can be overgeneralized and clumsy and writing your own.

nilton61: strtol() can convert strings of any base into binaries. But since it is that general it can be a bit clumsy. And the conversion is, as i demonstrated, quite simple. So its a balance between using general lib functions which can be overgeneralized and clumsy and writing your own.

I see, that does make sense. Thanks for the help!