1

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';
   }
}
5
  • The homework tag is obsolete. See tag info. Mar 1, 2013 at 19:35
  • Try changing buf to unsigned char buf[NUMCHAR]; and then doing your cast at inFile.read((char*)buf, NUMCHAR); Mar 1, 2013 at 19:40
  • @RobertMason, tried this, output was the same as (unsigned char)
    – Joe Pitz
    Mar 1, 2013 at 19:43
  • And if you cast to unsigned when you output? Mar 1, 2013 at 19:47
  • see above, I need to display as 2 digit hex number, not negative number. Thanks
    – Joe Pitz
    Mar 1, 2013 at 19:54

4 Answers 4

2

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

1
  • Accepting an answer is SO's way of saying thanks :-) @JoePitz Mar 1, 2013 at 19:53
1
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]);
        }
    }
}
3
  • Thanks, Homework assignment indicates we have to read [NUMCHAR] and print [NUMCHAR] bytes per line.
    – Joe Pitz
    Mar 1, 2013 at 19:47
  • endian issues is next lesson ;-)
    – Joe Pitz
    Mar 1, 2013 at 19:50
  • Thanks, I will try the static_cast, Next class is more into C++ internals.
    – Joe Pitz
    Mar 1, 2013 at 19:56
0

You can get rid of the sign extension by masking off the high bits:

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

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))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.