简体   繁体   中英

C++ sign extension

I am working on a homework problem, printing from a binary file. I have searched and found out that my problem is a sign extension problem.

In c the correct action would be to cast to an (unsigned char)

I have tried this solution and it does not work with cout

output with (unsigned) is:

4D 5A FFFFFF90 00 03 00 00 00 04 00 00 00 FFFFFFFF FFFFFFFF 00 00 

output with (unsigned char) is:

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

Any guidance would be most helpful;

Here is the code:

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

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

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

I would recommend do this character by character, the reason being there are all sorts of endian issues I would rather not even think about when dealing with rinterpretive int conversions. The std::ifstream will buffer the chars for you anyway (as will your OS likely too as well).

Notice how we take in the file stream as the more generic std::istream this allows us to pass in any type of istream including std::istringstream , std::cin and std::ifstream .

for example:

 ListHex(std::cin); 

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

would hex you user input.

edit

Using a buffer

void ListHex(std::istream& inFile) {
    // declare variables

    char arr[NUMCHAR];

    while(inFile.read(arr, NUMCHAR)) {
        for(std::size_t i=0; i!=NUMCHAR; ++i) {
            std::cout << std::setw(2) << std::hex 
                      << static_cast<int>(arr[i]);
        }
    }
}

您可以通过屏蔽高位来摆脱符号扩展:

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

std::cout prints unsigned char as a character, not an integer. You can perform two casts here – something along the lines of:

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

Alternatively, use an unsigned char buffer and a single cast:

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

Edit: A mask should not be used here as it assumes a particular character size. The following are equivalent only when CHAR_BIT is 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM