C++ - 显示来自 ARGB 值的参数不起作用?

C++ - Displaying Parameters from an ARGB Value not Working?

我正在开发一个简单的控制台应用程序,您可以在其中输入 ARGB 值的十六进制代码,应用程序会告诉您该值的红色、绿色、蓝色和 Alpha 内容(共 255 个)。

例如:

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

这通过对参数值使用按位 AND 运算符和一个 bitComparison 值来为 ARGB 值中的每个参数挑选出位,该值在位所在的相应位置打开了一组 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 . . .

这个编译很好并给出了预期的输出(使用 g++ c++11 支持编译):

#include <iostream> //For input/output operations
#include <cstdint>

void introduce()
{
    std::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()
{
    std::cout << "\n\nPlease enter an ARGB value: ";
    std::uint32_t value;
    std::cin >> std::hex >> value;
    std::cout << std::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();

    std::uint32_t ARGBValue{ getValue() };

    std::cout << std::dec << "\n\nThe selected value has the following parameter values (out of 255):\n- Alpha:\t" 
              << ((ARGBValue >> 24) & 0xFF)
              << "\n- Red:\t\t" 
              << ((ARGBValue >> 16) & 0xFF)
              << "\n- Green:\t" 
              << ((ARGBValue >> 8) & 0xFF) 
              << "\n- Blue:\t\t" 
              << (ARGBValue & 0xFF) 
              << "\n\n";

    return 0;
}