7

Consider the following :

#include <vector>
#include <string>
#include <iostream>

#include <boost/format.hpp>
#include <boost/assign.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/assign/std/vector.hpp>

using namespace std;

typedef unsigned char byte;
typedef vector<byte> byte_array;

const byte_array bytes = list_of(0x05)(0x04)(0xAA)(0x0F)(0x0D);

int main()
{
    const string formatter = "%1%-%2%-%3%-%4%-%5%";
    const string result = (format(formatter)
                           % bytes[0]
                           % bytes[1]
                           % bytes[2]
                           % bytes[3]
                           % bytes[4]
                                    ).str();
    cout << result << endl;
    return 0;
}

I'd like to see the result printed as : "05-04-AA-0F-0D" . What do I need to do to the formatter string to achieve that ?

1
  • @Maciek I know it's 2 years late, but here is what I do in these situations (good for printf in C as well I think) ... If I want to keep my original structure (bytes) and I usually do not mess with modifiers (setfill, setbase), I cast each byte with (unsigned int)(unsigned char).
    – nhed
    Aug 7, 2011 at 13:59

3 Answers 3

10

Compiled and tested:

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

using namespace std;
using namespace boost;

int main()
{
    unsigned int arr[5] = { 0x05, 0x04, 0xAA, 0x0F, 0x0D };

    cout << format("%02X-%02X-%02X-%02X-%02X")
                % arr[0]
                % arr[1]
                % arr[2]
                % arr[3]
                % arr[4]
         << endl;
}
3
  • In this answer the element type is different from the original question's element type. Specifically, the original's element type is 'unsigned char' whereas the answer's is 'unsigned int'. I'm having an issue using boost::format with unsigned char where it's formatting it as a char rather than an integer. Were this iostreams that would be okay, but with format it should be outputting the way I tell it I want it, not according to type safety. Yes, I could cast, but that's seems counter. Was the change in element type for similar reasons? Jul 6, 2011 at 17:15
  • @Chris Cleeland - Ah, I was probably asleep at the wheel. Working on bytewise protocols, I've hit this a couple of times since then. 0x61 comes out as 'a' instead of 97, as you say. The (unfortunately) correct response seems to be to wrap such as int(arr[0]). I do wonder if this is by design, because it's a departure from the behaviour of printf.
    – Kaz Dragon
    Jul 7, 2011 at 10:21
  • thanks for the clarification! too bad boost::format doesn't do the cast for you. Hmmm... Jul 12, 2011 at 0:09
2

Using iostream only it can be done by manipulating output stream. Following simple example shows what can be done.

#include <iostream>
#include <iomanip>

unsigned char a = 0x05;
unsigned char b = 0xA8;

using namespace std;

int main()
{    
    std::cout << setbase(16) << setfill('0') << setw(2) <<
         (short)a << "-" << (short)b << std::endl;
}

Output will be: 05-a8

Boost::format also allows to use same format manipulators.

Sample example from boost::format page illustrates its usage.

using boost::format;
using boost::io::group;
// Using manipulators, via 'group' :
cout << format("%2% %1% %2%\n")  % 1   % group(setfill('X'), hex, setw(4), 16+3) ;
// prints "XX13 1 XX13\n"

This should help you get what you need.

2

Boost formatter respects printf formatting. Have you tried:

const string formatter = "%02x-%02x-%02x-%02x-%02x";

?

May also want to add an "hh" before the x, to indicate that the value is 8 bits.

Your Answer

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

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