C++ c++;,stl。是否将带有rgb颜色的字符串转换为3个整数值?

C++ c++;,stl。是否将带有rgb颜色的字符串转换为3个整数值?,c++,string,stl,hex,rgb,C++,String,Stl,Hex,Rgb,字符串看起来像“#123456”,结果应该是: int r = 0x12; int g = 0x34; int b = 0x56; 或者,在c++中,此任务的情况正好相反: 我知道我可以将当前字符串拆分为3个子字符串,但如何将它们实现为十六进制值?您应该首先获取数字,去掉# 然后,您可以使用stringstream: #include <sstream> ... { using namespace std; stringstream ss("123456"); i

字符串看起来像“#123456”,结果应该是:

int r = 0x12;
int g = 0x34;
int b = 0x56;
或者,在c++中,此任务的情况正好相反:


我知道我可以将当前字符串拆分为3个子字符串,但如何将它们实现为十六进制值?

您应该首先获取数字,去掉#

然后,您可以使用stringstream:

#include <sstream>

...

{
  using namespace std;
  stringstream ss("123456");
  int num;

  ss >> hex >> num;
}
编辑:不知道std::stoi(可从C++11获得)。谢谢@juanchopanza。使用它,您可以更改代码的第一部分:

#include <string>

...
{
  using namespace std;
  int num = stoi("123456", 0, 16);

  int r = ....
}
#包括
...
{
使用名称空间std;
int num=stoi(123456,0,16);
int r=。。。。
}

您可以创建三个子字符串,将它们转换为int,然后使用std::hex


请参阅。

一旦将每个部分组成字符串,请使用
strtol()
将其转换为数字。请确保使用16作为“base”参数,因为您的值是十六进制的。

请在将字符串拆分为3个独立的数字后重试。

这里有一个简单的解决方案:

unsigned int str_to_hex(char const * p, char const * e) noexcept
{
    unsigned int result = 0;
    while (p != e)
    {
        result *= 16;
        if ('0' <= *p && *p <= '9') { result += *p - '0';  continue; }
        if ('A' <= *p && *p <= 'F') { result += *p + 10 - 'A'; continue; }
        if ('a' <= *p && *p <= 'f') { result += *p + 10 - 'a'; continue; }
        return -1;
    }
}

std::tuple<unsigned char, unsigned char, unsigned char>
str_to_col(std::string const & s)
{
    if (str.length() != 7 || s[0] == '#') { /* error */ }

    auto r = str_to_hex(str.data() + 1, str.data() + 3);
    auto g = str_to_hex(str.data() + 3, str.data() + 5);
    auto b = str_to_hex(str.data() + 5, str.data() + 7);

    if (r == -1 || g == -1 || b == -1) { /* error */ }

    return {r, g, b};
}

除了自制的
str_to_hex
函数,您还可以使用标准库的转换函数,例如
std::strtool(s.substring(1),nullptr,16)
使用标准库的
std::hex

#include <iostream>
#include <sstream>

int main()
{
   // The hex code that should be converted ...
   std::string hexCode;
   std::cout << "Please enter the hex code: ";
   std::cin >> hexCode;

   // ... and the target rgb integer values.
   int r, g, b;

   // Remove the hashtag ...
   if(hexCode.at(0) == '#') {
      hexCode = hexCode.erase(0, 1);
   }

   // ... and extract the rgb values.
   std::istringstream(hexCode.substr(0,2)) >> std::hex >> r;
   std::istringstream(hexCode.substr(2,2)) >> std::hex >> g;
   std::istringstream(hexCode.substr(4,2)) >> std::hex >> b;

   // Finally dump the result.
   std::cout << std::dec << "Parsing #" << hexCode 
      << " as hex gives (" << r << ", " << g << ", " << b << ")" << '\n';
}
#包括
#包括
int main()
{
//应该转换的十六进制代码。。。
字符串十六进制码;
std::cout>hexCode;
//…和目标rgb整数值。
int r,g,b;
//删除标签。。。
if(hexCode.at(0)='#'){
hexCode=hexCode.erase(0,1);
}
//…并提取rgb值。
std::istringstream(hexCode.substr(0,2))>>std::hex>>r;
std::istringstream(hexCode.substr(2,2))>>std::hex>>g;
std::istringstream(hexCode.substr(4,2))>>std::hex>>b;
//最后转储结果。

std::cout另一种方法是:uint8\u t三元组到字符串

#include <string>
#include <iostream>
#include <sstream>

std::string to_hex_string(uint8_t red, uint8_t green, uint8_t blue) {
    // r, g, b -> "#RRGGBB"
    std::ostringstream oss;
    oss << '#';
    oss.fill('0');
    oss.width(6);
    oss << std::uppercase << std::hex << ((red << 16) | (green << 8) | blue);
    return oss.str();
}
#包括
#包括
#包括
标准::字符串到十六进制字符串(uint8_t红色、uint8_t绿色、uint8_t蓝色){
//r,g,b->“RRGGBB”
std::ostringstream oss;

oss See和friends.@juanchopanza:这些函数没有处理非十进制基数的方便方法,是吗?@juanchopanza:看起来我的库不完整或被删减了,我没有这个function@KerrekSB对我来说很方便。
inti=std::stoi(“0x20”,0,16);
inti=std::stoi(“20”,0,16);
@user2083364它需要C++11库支持。根据编译器的不同,您可能需要打开它,或者根本没有。为什么要重新编写一个已经用很多方法完成的函数,比如将字符串转换为十六进制的函数?@rcrmn:无聊?渴望强大、黄金和中立?对引用字符串API缺乏信心?O好的。那你就可以开始了!但是它们大部分都工作得很好。@rcrmn:我(和其他许多人)真正缺少的一件事是以统一方式引用现有字符串(和数组)的方法。对于
string\u ref
array\u ref
类(我认为Boost有一个),但当然,这样一个类不会像大多数标准容器那样是一个“拥有值”,因此需要用户特别注意。但它非常值得拥有。小字符串优化可能会使用
s.substring(1)
相当有效,但…有时人们会觉得应该做得更好:-)好吧,是的,但为op所需编写这么多代码有点太费劲了。不过,我可以看到
字符串和类似类的用法。感谢您的参考,有时人们只会坚持它给出的内容,而不去寻找otE57139显示(229,87144),但在photoshop和计算器中显示(229,113,57)。我的意思是std::stoi之前的代码将所有出现的0xff替换为0x100,我会批准它修复,很抱歉。请注意,
std::stoi
系列函数从C++11开始就可用。这可能值得一提。正如我已经提到的,我的编译器是Apple LLVM编译器4.2。如何检测它是否使用C++11?
#include <iostream>
#include <sstream>

int main()
{
   // The hex code that should be converted ...
   std::string hexCode;
   std::cout << "Please enter the hex code: ";
   std::cin >> hexCode;

   // ... and the target rgb integer values.
   int r, g, b;

   // Remove the hashtag ...
   if(hexCode.at(0) == '#') {
      hexCode = hexCode.erase(0, 1);
   }

   // ... and extract the rgb values.
   std::istringstream(hexCode.substr(0,2)) >> std::hex >> r;
   std::istringstream(hexCode.substr(2,2)) >> std::hex >> g;
   std::istringstream(hexCode.substr(4,2)) >> std::hex >> b;

   // Finally dump the result.
   std::cout << std::dec << "Parsing #" << hexCode 
      << " as hex gives (" << r << ", " << g << ", " << b << ")" << '\n';
}
#include <string>
#include <iostream>
#include <sstream>

std::string to_hex_string(uint8_t red, uint8_t green, uint8_t blue) {
    // r, g, b -> "#RRGGBB"
    std::ostringstream oss;
    oss << '#';
    oss.fill('0');
    oss.width(6);
    oss << std::uppercase << std::hex << ((red << 16) | (green << 8) | blue);
    return oss.str();
}