9

I'm completely new to C/C++ and I am trying to figure out how to convert a String argument that would be in the form of a html style rgb hex such as "#ffffff" and turn that into 3 integers vars

I'm really not sure where to being.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Polygon Pusher
  • 2,865
  • 2
  • 27
  • 32

3 Answers3

19

All you need to do is convert the string to integers and then split them into three separate r, g, b values.

string hexstring = "#FF3Fa0";

// Get rid of '#' and convert it to integer
int number = (int) strtol( &hexstring[1], NULL, 16);

// Split them up into r, g, b values
int r = number >> 16;
int g = number >> 8 & 0xFF;
int b = number & 0xFF;

You may want to have a look at this question as well.


Edit (thanks to James comments):

For some machine (e.g. Arduino (Uno)), ints are 16 bits instead of 32. If red values are dropping for you, use a long instead.

string hexstring = "#FF3Fa0";

// Get rid of '#' and convert it to integer
long number = strtol( &hexstring[1], NULL, 16);

// Split them up into r, g, b values
long r = number >> 16;
long g = number >> 8 & 0xFF;
long b = number & 0xFF;

Edit (an even safer version, use strtoll instead of strtol):

long long number = strtoll( &hexstring[1], NULL, 16);

// Split them up into r, g, b values
long long r = number >> 16;
long long g = number >> 8 & 0xFF;
long long b = number & 0xFF;
Community
  • 1
  • 1
Yuchen
  • 30,852
  • 26
  • 164
  • 234
  • This one looks the best to me so far but the problem is I'm starting with a type of String and I'm getting cannot convert 'String' to 'const char*' – Polygon Pusher May 10 '14 at 04:30
  • You can convert a string to `const char*` by the following `string v1 = "#FF3Fa0"; const char* v2 = v1.c_str()`. And also, this `&hexstring[1]` simply means that you take the address of `hexstring[1]`, it doesn't matter whether it is `string` or `const char*`. I have modified the answer. – Yuchen May 10 '14 at 12:52
  • 2
    Thanks for the answer Yuchen! I want to note that for my Arduino (Uno) ints are 16 bit instead of 32. If red values are dropping for you, try using a long instead. – James Mar 02 '15 at 08:26
  • @James, this is very helpful! Thank you. I added your comments in the answer. – Yuchen Mar 02 '15 at 14:36
  • when i run string hexstring = "#FF3Fa0"; i get 'string' was not declared in scope – Jim True Jul 03 '17 at 00:52
  • @JimTrue please add `#include ` – Yuchen Jul 03 '17 at 00:57
2

First, you need to parse your value. You may do that this way:

void parse_hex(char* a, char* b, char* c, const char* string) {
    //certainly not the most elegant way. Note that we start at 1 because of '#'
    a[0] = string[1];
    a[1] = string[2];
    b[0] = string[3];
    b[1] = string[4];
    c[0] = string[5];
    c[1] = string[6];
}

Then, you will convert each string into it's correspondent integer. You can learn how to do that from this answer.

Community
  • 1
  • 1
0
#include <stdlib.h>
#include <iostream>

int main()
{
   char const* str = "#FF9922";
   char red[5] = {0};
   char green[5] = {0};
   char blue[5] = {0};

   red[0] = green[0] = blue[0] = '0';
   red[1] = green[1] = blue[1] = 'X';

   red[2] = str[1];
   red[3] = str[2];

   green[2] = str[3];
   green[3] = str[4];

   blue[2] = str[5];
   blue[3] = str[6];

   int r = strtol(red, NULL, 16);
   int g = strtol(green, NULL, 16);
   int b = strtol(blue, NULL, 16);

   std::cout << "Red: " << r << ", Green: " << g << ", Blue: " << b << std::endl;
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270