C++:将数组数据转化为string

时间:2025-03-14 19:35:04
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
	unsigned char buf[10] = {0, 0, 0, 0, 5, 0xF, 9, 0xA5, 0x30, 0x30};
	string output;

	for(int i = 0; i < 10; i++)
	{
		std::stringstream data;
		data << std::hex << (int)buf[i];

		cout << () << endl;

		if(buf[i] < 0x10)
		{
			output.push_back('0');
		}

		output += ();
	}

	cout << "output: " << output.c_str() << endl;

	return 0;
}
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
	unsigned char buf[10] = {0, 0, 0, 0, 5, 0xF, 9, 0xA5, 0x30, 0x30};
	string output;

	for(int i = 0; i < 10; i++)
	{
		std::stringstream data;
		('0');
		(2);

		data << std::hex << (int)buf[i];

		cout << () << endl;

		output += ();
	}

	cout << "output: " << output.c_str() << endl;

	return 0;
}

 

uidh2917@hiv3209u:~/Source$ g++ 
uidh2917@hiv3209u:~/Source$ 
uidh2917@hiv3209u:~/Source$ 
uidh2917@hiv3209u:~/Source$ 
uidh2917@hiv3209u:~/Source$ ./
0
0
0
0
5
f
9
a5
30
30
output: 00000000050f09a53030