1
我正在開發一個簡單的控制檯應用程序,您可以在其中輸入ARGB值的十六進制代碼,並且應用程序會告訴您255個值中的紅色,綠色,藍色和Alpha內容。C++ - 顯示來自ARGB值的參數不起作用?
例如:
Enter a 32-bit RGBA color value in hexadecimal (e.g. FF7F3300): FF7F3300
Your color contains:
255 of 255 red
127 of 255 green
51 of 255 blue
0 of 255 alpha
從理論上講,它是應用程序應該如何工作。但事實並非如此。它爲每種顏色顯示0。
我在C#應用程序中使用了與此應用程序相同的代碼,除了經過語法調整以適應語言,並且它正常工作。
但是,由於我缺乏C++知識,即使經過半小時的思考,我也無法正確調試此應用程序。我用於此
我的C++代碼如下:
#include "stdafx.h" //Always imported for VisualStudio
#include <iostream> //For input/output operations
using namespace std; //To simplify code - no conflicting namespaces are being used, and yet the std namespace is used often
void introduce()
{
cout << "Welcome to 'ARGB to Decimal'! This program will take a hex ARGB colour value (such as FF7F3300) and output the colour parameters that this value stores.";
}
uint32_t getValue()
{
cout << "\n\nPlease enter an ARGB value: ";
uint32_t value;
cin >> hex >> value;
cout << hex << "You have selected an ARGB value of " << value;
return value; //Converted to hex due to bitwise operations that will be performed on this value
}
int main()
{
introduce();
uint32_t ARGBValue{ getValue() };
uint32_t bitComparison{ 0xFF000000 }; //Used as the right operand of a bitwise AND operator to single out the bits for each byte of the ARGB value (with its bits being shifted 8 bits to the right before the 2nd, 3rd, and 4th comparison, and so display the appropriate byte value for that parameter
cout << "\n\nThe selected value has the following parameter values (out of 255):\n- Alpha:\t"
<< ((ARGBValue & bitComparison) >> 24)
<< "\n- Red:\t\t" << ((ARGBValue & (bitComparison >>= 8)) >> 16)
<< "\n- Green:\t" << ((ARGBValue & (bitComparison >>= 8)) >> 8)
<< "\n- Blue:\t\t" << (ARGBValue & (bitComparison >>= 8))
<< "\n\n";
system("pause");
return 0;
}
這挑出在ARGB值中的每個參數的比特通過使用與該參數值的按位與運算符和一個bitComparison值,其具有在相應位置處開啓的一組8位的位,在該位置當前參數的位被單出。
bitComparison位在cout
語句內移位,因爲它正在執行。
如果有人能告訴我如何解決這個問題,我將不勝感激。
下面是示例輸出,它不工作:
Welcome to 'ARGB to Decimal'! This program will take a hex ARGB colour value (such as FF7F3300) and output the colour parameters that this value stores.
Please enter an ARGB value: FF7F3300
You have selected an ARGB value of ff7f3300
The selected value has the following parameter values (out of 255):
- Alpha: 0
- Red: 0
- Green: 0
- Blue: 0
Press any key to continue . . .
」到十六進制由於按位操作'您不必打擾,std :: dec完全適合這些情況。 – Papipone
此外,它看起來你用VS編碼。用調試器遍歷代碼並檢查(ARGBValue&(bitComparison >> = 8)...字段的值。 – Papipone