2017-03-17 43 views
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 . . . 
+0

”到十六进制由于按位操作'您不必打扰,std :: dec完全适合这些情况。 – Papipone

+0

此外,它看起来你用VS编码。用调试器遍历代码并检查(ARGBValue&(bitComparison >> = 8)...字段的值。 – Papipone

回答

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