2

I am trying to print hex values for member[0] and member[1] and integer values for _records in the same text file with the following code:

 std::ofstream myoutputfile;

 myoutputfile << std::hex << (int)(unsigned char)_member[0] << ' ';

 myoutputfile << std::hex << (int)(unsigned char)_member[1] << ' ';

 myoutputfile<<_records << std::endl;

But this code prints _records in hex too. I have tried:

myoutputfile<<(int)_records << std::endl;

But still it prints it in hex.

Can anyone help please.

hmjd
  • 120,187
  • 20
  • 207
  • 252
Deepika Sethi
  • 213
  • 1
  • 2
  • 10

4 Answers4

4

Set the format back to decimal using std::dec

myoutputfile<< std::dec << _records << std::endl;

As you've discovered, the base format specifiers are sticky, so there's no need to specify std::hex each time. Your code could be rewritten as follows to achieve the same effect.

std::ofstream myoutputfile;
myoutputfile << std::hex 
  << (int)(unsigned char)_member[0] << ' '
  << (int)(unsigned char)_member[1] << ' '
  << std::dec << _records << std::endl;

You could also have the resetting to previous state done automatically by using Boost IO State Savers.

#include <boost/io/ios_state.hpp>
#include <ios>
#include <iostream>
#include <ostream>

int  main()
{
    auto hex_printer = [](std::ostream& os, unsigned char byte) {
        boost::io::ios_flags_saver  ifs(os);
        os << std::hex << static_cast<unsigned>(byte) << ' ';
    };

    hex_printer(std::cout, 'A');
    std::cout << 42 << '\n';
}

Live demo

Praetorian
  • 106,671
  • 19
  • 240
  • 328
2

The base that's use for conversion from numbers to text is a state of the output stream rather than a function of the input type. You need to do:

myoutputfile<< std::dec << _records << std::endl;

This changes the state of the output stream to put _records using decimal.

  • Nitpick: this behavior doesn't really have to do with the state being associated with the stream object rather than the object being written to the stream. If that were the case [`std::setw`](http://en.cppreference.com/w/cpp/io/manip/setw) would persist too, but that resets after most formatted input/output. The rest of them persist because they're required to do so. See [this](http://stackoverflow.com/a/1533222/241631) answer. – Praetorian Jan 17 '14 at 15:32
  • I see what you are saying. My point wasn't that the radix state wasn't necessarily reset after the next invocation of stream << data, but rather that the (int) cast couldn't possibly reset the radix state. –  Jan 17 '14 at 17:29
1

std::hex tells an ostream to print everything in hex from that point on. The equivalent that will return it to decimal is std::dec. Try the following:

std::ofstream myoutputfile;
myoutputfile << std::hex;
myoutputfile << _member[0] << ' ';
myoutputfile << _member[1] << ' ';
myoutputfile << std::dec << _records << std::endl;

Check out http://www.cplusplus.com/reference/ios/hex/ as an additional reference.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
0

you can use fprintf, which prints formatted text:

frintf("%x %x %d\n",_member[0],_member[0],_records);

Hope this helps

Pandrei
  • 4,843
  • 3
  • 27
  • 44