字符串看起來像「#123456」,其結果應該是:C++,stl。將rgb顏色的字符串轉換爲3個int值?
int r = 0x12;
int g = 0x34;
int b = 0x56;
或者乾脆逆在C這個任務++:
How to convert RGB int values to string color
我知道我可以分割當前字符串轉換爲3個子字符串,但如何將它們實現爲十六進制值?
字符串看起來像「#123456」,其結果應該是:C++,stl。將rgb顏色的字符串轉換爲3個int值?
int r = 0x12;
int g = 0x34;
int b = 0x56;
或者乾脆逆在C這個任務++:
How to convert RGB int values to string color
我知道我可以分割當前字符串轉換爲3個子字符串,但如何將它們實現爲十六進制值?
你應該首先得到該號碼,剝離#。
然後,您可以使用字符串流:
#include <sstream>
...
{
using namespace std;
stringstream ss("123456");
int num;
ss >> hex >> num;
}
最後除以得到每個組件:
// Edit: I did it from memory and got it wrong, its % 0x100, not % 0xff.
int r = num/0x10000;
int g = (num/0x100) % 0x100;
int b = num % 0x100;
編輯:不知道的std :: Stoi旅館(可使用C++ 11)。感謝@juanchopanza。有了它你可以改變代碼的第一部分:
#include <string>
...
{
using namespace std;
int num = stoi("123456", 0, 16);
int r = ....
}
這是錯誤的。 E57139顯示(229,87,144),但在Photoshop和計算器中使用它是(229,113,57)。我的意思是你的代碼之前std :: stoi – user2083364
用0x100代替0xff的所有發生,我會批准它 – user2083364
修復,對不起。 – rcrmn
你可以做三個子字符串,將它們轉換爲整數,然後使用std :: hex?
請參閱this。
將每個部分放在一個字符串中後,使用strtol()
將其轉換爲數字。請確保使用16作爲「基本」參數,因爲您的值是十六進制。
嘗試sscanf()將您的字符串拆分爲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};
}
如果您在別處驗證你的輸入,你可以縮寫的最後一個函數說只有return { str_to_hex(...), ... };
。
或者,你並不需要在所有分割字符串:
std::string s = "#123456";
auto n = str_to_hex(s.data() + 1, s.data() + 7);
auto r = n/0x10000, g = (n/0x100) % 0x100, b = n % 0x10000;
取而代之的是自制str_to_hex
功能,你也可以使用標準庫的轉換功能,如std::strtoul(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';
}
參見[性病:: Stoi旅館(http://en.cppreference.com/w/cpp/string/basic_string/stol)和朋友。 – juanchopanza
@juanchopanza:這些函數沒有處理非十進制數字的方便方法,是嗎? –
@juanchopanza:它也看起來像我的圖書館是不完整或切割,我沒有這個功能 – user2083364