下面的程序主要是将字符串的数组,以16进制的形式输出,用空格隔开。
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
char *temp = new char[8];
temp[0] = 01;
temp[1] = 2;
temp[2] = 03;
temp[3] = 10;
temp[4] = 11;
temp[5] = 7;
temp[6] = 9;
std::stringstream ss;
for (int i = 0; i<7; i++)
{
int tm = temp[i];
ss << std::hex << std::setw(2) << std::setfill('0') << tm;//见下文注释
ss << " ";
}
string c = ss.str();
string d;
transform(c.begin(), c.end(), back_inserter(d), ::toupper);//将小写转化为大写
std::cout << "string is : " << d << std::endl;
system("pause");
return 0;
}
ss << std::hex << std::setw(2) << std::setfill('0') << tm。这里的stringstream ss;定义的字符串流,是将后面的字符串按照对象存入到ss中并以空格键结束,如01为一个对象,02为一个对象,对象之间都有空格。std::hex表示16进制输出流,即后面输出的为16进制的形式,setw(2)以两个字符的形式输出,setfill(0)不够两个字符的用0填充。最后的输出结果为:
愿我们都能被世界温柔以待!!!