如何使用boost :: format生成十六进制输出?

Mac*_*iek 6 c++ boost

考虑以下 :

#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;
}
Run Code Online (Sandbox Code Playgroud)

我希望看到结果打印为:"05-04-AA-0F-0D".我需要做什么才能实现格式化程序字符串?

Kaz*_*gon 9

编译和测试:

#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;
}
Run Code Online (Sandbox Code Playgroud)