How to convert String like "FF00FF" to long (RGB) ?

Hi all, I have an issue with my program. I m trying to convert a string like "FF00FF" to integer like 16711935 to retrieve RGB code color.

If someone could help I appreciate it.

Regards

If someone could help I appreciate it.

My crystal ball suggests that line 27 needs some major work.

Of course, it's cracked, and suggested that the Mariners would win the Super Bowl, so take it's advice with a grain of salt.

You would do it the same way on an Arduino as you would do it with a pencil and paper. Each column represents the number multiplied by the appropriate power of 16 - 0, 1, 2 up to 5.

...R

Lol. I already searched and i dont find how to convert string to hex. Very low docs :/

My problem is not tout convert hex to rgb but string to hex.

i dont find how to convert string to hex

The concept of string to hex is what you need to explain. All data is stored internally in binary.

Where is your code? How can we help you fix code we can't see?

If you have a string that contains hex characters, like 0xFF00FF, you can convert that to a numeric value using strtoul(). The internal storage in the variable will NOT be in hex. It will be in binary, but you can print it in any base you like. The function that uses the value in the variable expects the value to be in binary.

I have something like this:

String s = "FF00FF"; unsigned long l;

My question is: * how to convert s to l.

I don't think I can use strtoul because s contains "FF00FF" and not 0xFF00FF.

An idea ?

jhdscript: Lol. I already searched and i dont find how to convert string to hex. Very low docs :/ My problem is not tout convert hex to rgb but string to hex.

That string is already Hex. Hex is not a value. It is a representation of a value. Each character represent a 4-bit value, 0-15, or 0000 to 1111. Each character, converted to a value, and placed in byte, an int, a long, signed or unsigned, in the right place in that data type, will yield a value representing the hex string.

You can place the converted values with bitwise operators or by multiplying and adding.

You would be MUCH better off using C strings (char arrays).

You need to take each element of the string (which is why a char array would be easier to use) starting with the right-most or least significant one. Then you need to convert the character to its number equivalent. Then you need to process that number to build up the full value.

To convert the character '0' to the number zero just subtract the Ascii value for the character '0' which is 48. To convert the character 'A' to the number 10 subtract 55 because 'A' has the Ascii value 65.

...R

Actually, it’s probably easier to start with the most significant character.
I can’t take credit for this function, but here’s what I use:

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

  char hx[] = {"FF00FF"};
  long rgb = 0;

  for (int i= 0; i < strlen(hx); i++) {
    rgb = (rgb * 16) + x2b(hx[i]);
  }
  Serial.println(rgb);

}

void loop() {
}

byte x2b( char c) {
  if (isdigit(c)) {  // 0 - 9
    return c - '0';
  } 
  else if (isxdigit(c)) { // A-F, a-f
    return (c & 0xF) + 9;
  }

}

thanx for the code it works good now