Hello,
i have some decimal numbers and want to put them hexadecimal in a string stream. Every Hex number should consist of 2 chars^^. I can't find a flag for it.

0__1__2__3__4__5__6__7__8__9__a__b__c__d__e__f__10__11__12__13__14__15__16__17__18__19__1a__1b__1c__1d__1e__1f__20__21_

should be

00__01__02__03__04__05__06__07__08__09__0a__...
#include <cstdio>
#include <iostream>
#include <sstream>
#include <string>
#include <iomanip>

using namespace std;

int main (){

 int number = 16;
 string test;

 stringstream out (stringstream::in | stringstream::out);

 for (int i = 0; i < 255; i++){
 	out << hex << i << "__";
 }

 test = out.str();
 cout << test;

return 0;
}

Recommended Answers

All 2 Replies

With sstream-hex you have to add the zero.

Something like:

vector<string> test; //or array, doesn't matter
...
if(test[i].size() < 2)
  test[i].insert(0, "0");

I often zero pad things like this:

std::string ZeroPad(const char num, const unsigned int rep)
{
	std::stringstream Filled;
	Filled << std::setfill('0') << std::setw(rep) << num;
			
	return Filled.str();
}

but maybe inserting a '0' like MosaicFuneral said is more appropriate in this case?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.