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

时间:2017-03-17 21:32:14

标签: c++ bit-manipulation argb

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

例如:

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 . . .

1 个答案:

答案 0 :(得分:2)

编译正常并给出预期的输出(使用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;
}