将字符串“#FFFFFF”转换为3 int

时间:2020-12-05 15:44:23

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

我对C/ c++完全不熟悉,我正在研究如何将一个字符串参数转换为html样式的rgb十六进制,如“#ffffff”,并将其转换为3个整数vars

I'm really not sure where to being.

我真的不知道去哪里。

3 个解决方案

#1


13  

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

你所需要做的就是将字符串转换成整数,然后将它们分割成三个独立的r g b值。

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):

编辑(感谢James的评论):

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.

对于某些机器(例如Arduino (Uno)), ints是16位,而不是32位。如果红色值为您降低,请使用长值。

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):

编辑(更安全的版本,使用strtoll代替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;

#2


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.

然后,将每个字符串转换成对应的整数。你可以从这个答案中学到怎么做。

#3


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;
}

#1


13  

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

你所需要做的就是将字符串转换成整数,然后将它们分割成三个独立的r g b值。

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):

编辑(感谢James的评论):

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.

对于某些机器(例如Arduino (Uno)), ints是16位,而不是32位。如果红色值为您降低,请使用长值。

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):

编辑(更安全的版本,使用strtoll代替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;

#2


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.

然后,将每个字符串转换成对应的整数。你可以从这个答案中学到怎么做。

#3


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;
}