Wie produzieren hex-Ausgabe mit boost::format?

Folgendes :

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

Ich möchte sehen, wie das Ergebnis gedruckt als : "05-04-AA-0F-0D" . Was muss ich tun, um die formatter-string zu erreichen ?

bytes[5] ist out of range
whoops, my bad - bearbeitet
Ich weiß, dass es 2 Jahre zu spät, aber hier ist, was ich in diesen Situationen (gut für printf in C auch, denke ich) ... Wenn ich will, dass meine ursprüngliche Struktur (bytes) und ich in der Regel nicht mit Zohan an Modifikatoren (setfill, setbase), treibe ich jedes byte mit (unsigned int)(unsigned char).

InformationsquelleAutor Maciek | 2009-11-04

Schreibe einen Kommentar