将字符串数组以16进制的形式输出

下面的程序主要是将字符串的数组,以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填充。最后的输出结果为:                   

 愿我们都能被世界温柔以待!!! 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值