C ++符号扩展

时间:2013-03-01 19:32:26

标签: c++

我正在处理家庭作业问题,从二进制文件打印。 我搜索过并发现我的问题是符号扩展问题。

在c中,正确的操作是转换为(unsigned char)

我尝试过这个解决方案,它不适用于cout

带有(unsigned)的

输出是:

4D 5A FFFFFF90 00 03 00 00 00 04 00 00 00 FFFFFFFF FFFFFFFF 00 00 
带有(unsigned char)的

输出是:

0M 0Z 0ê 0� 0 0� 0� 0� 0 0� 0� 0� 0ˇ 0ˇ 0� 0� 

任何指导都会有所帮助;

以下是代码:

void ListHex(ifstream &inFile)
{
    // declare variables
    char buf[NUMCHAR];
    unsigned char bchar;

    while(!inFile.eof())
    {
       inFile.read(buf,NUMCHAR);
       for (int count = 0; count < inFile.gcount(); ++count)
       {

        cout << setfill('0') << setw(2) << uppercase << hex << 
           (unsigned)buf[count] << ' ';
       }
       cout << '\n';
   }
}

4 个答案:

答案 0 :(得分:2)

cout <<setfill('0') << setw(2) << uppercase << hex << (0xFF & buf[count])

怎么样?

答案 1 :(得分:1)

void ListHex(std::istream& inFile) {
    // declare variables
    char c;
    while(inFile >> c) {
        std::cout << std::setw(2) << std::hex 
                  << static_cast<int>(c);
    }
}

我建议按字符执行此字符,原因是存在各种各样的字节序问题,我宁愿在处理rinterpretive int转换时也不会考虑。 std::ifstream无论如何都会缓冲你的角色(你的操作系统也可能也是如此)。

请注意我们如何接收文件流作为更通用的std::istream,这允许我们传递任何类型的istream,包括std::istringstreamstd::cin和{{1 }}

例如:

std::ifstream

会使用户输入十六进制。

修改

使用缓冲区

 ListHex(std::cin); 

 std::istringstream iss("hello world!");
 ListHex(iss);

答案 2 :(得分:0)

你可以通过屏蔽高位来消除符号扩展:

(((unsigned) buf[count)) & 0xff)

答案 3 :(得分:0)

std :: cout将unsigned char打印为字符,而不是整数。你可以在这里进行两次演员表演 - 以下几点:

static_cast <int> (static_cast <unsigned char> (buf[count]))

或者,使用unsigned char缓冲区和单个强制转换:

void ListHext(ifstream& inFile)
{
    unsigned char buf[NUMCHAR];
    while (inFile.read(reinterpret_cast <char*> (&buf[0]), NUMCHAR))
    {
        for (int i=0; i < NUMCHAR; ++i)
            cout << ... << static_cast <int> (buf[i]) << ' ';
        cout << endl;
    }
}

编辑: 这里不应使用掩码,因为它假设特定的字符大小。仅当CHAR_BIT为8时,以下内容才相同:

// bad examples
x & 0xFF // note - implicit int conversion
static_cast <int> (x) & 0xFF // note - explicit int conversion

// good example
static_cast <int> (static_cast <unsigned char> (x))