Boost logo

Boost :

Subject: [boost] Boost format hex issue..
From: Rob Kramer (robk_at_[hidden])
Date: 2010-07-14 04:48:22


Hi,

I'm having an issue with boost::format, in that I can't get it to format a
value as hexadecimal if that value is of float type. For example, the little
test program below prints:

"As int: 0018, 0x0012, as float: 0018, 0x0018"

This is as intended, I suppose, since floats can't printed in hex form
anyhow. But is there anyway I can get the 'x' format type attribute to
interpret the value as integer? Simply casting it to int is not an option,
because the way my float value is interpreted should depend only on the
format string.

I added a hack to my application that fixes it for now, but that doesn't
seem very correct:

  boost::format (..) fmt;
  double v = ..;

  if (fmod (v, 1.0) == 0.0)
      fmt % (int) v;

  else
      fmt % v;

Cheers!

     Rob

#include <iostream>
#include <iomanip>
#include <boost/format.hpp>

using namespace std;

int main()
{
    cout << "As int: "
         << boost::format ("%1$04d, 0x%1$04x") % 18
         << ", as float: "
         << boost::format ("%1$04d, 0x%1$04x") % (double) 18
         << endl;
}


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk