【问题标题】:C++ - Displaying Parameters from an ARGB Value not Working?C++ - 显示来自 ARGB 值的参数不起作用?
【发布时间】:2017-03-17 21:32:13
【问题描述】:

我正在开发一个简单的控制台应用程序,您可以在其中输入一个 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;
}

这通过对参数值和 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 . . .

【问题讨论】:

  • '由于按位运算转换为十六进制'您不必为此烦恼,std::dec 非常适合这些情况。
  • 另外,您似乎使用 VS 进行编码。使用调试器单步执行您的代码并检查 (ARGBValue & (bitComparison >>= 8) ... 字段的值。

标签: c++ bit-manipulation argb


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-07-25
    • 2021-06-29
    • 1970-01-01
    • 2012-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多