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;
}

Dani AI

Generated

— the behaviour you see is normal: the std::hex manipulator switches the stream to hexadecimal but does not force a fixed field width, so values 0..15 print as a single digit. ’s post‑processing fix (insert a "0" when length < 2) will work for simple cases; pointed toward zero‑padding via stream manipulators which is the standard iostream solution. A couple of practical, robust tips and an alternative implementation follow.

A compact, portable approach that avoids fiddling with stream state is to format each byte with a small C-style formatter and append separators. This is simple, fast and avoids surprises when the input type is char or negative:

#include <string>
#include <vector>
#include <cstdio>

std::string hex_series(const std::vector<int>& values) {
    std::string out;
    char buf[3]; // two hex digits + terminating NUL
    for (int v : values) {
        std::snprintf(buf, sizeof(buf), "%02x", v & 0xFF);
        out += buf;
        out += "__";
    }
    if (!out.empty()) out.resize(out.size() - 2); // drop trailing separator
    return out;
}

Notes and gotchas:

  • Always mask/cast values (e.g. v & 0xFF or static_cast<unsigned>(v)) so sign extension or negative values don’t produce unexpected hex output.
  • If you prefer iostreams, set a two‑width and a fill character before each insertion (width resets after each formatted write); the stream hex flag is sticky, so restore std::dec when needed. See the manipulators reference for details: std::hex, std::setw, std::setfill.
  • With C++20 you can use std::format("{:02x}", value) for concise code; see std::format.
  • Use std::uppercase if you need 0A instead of 0a.

This gives predictable two‑char hex bytes across platforms and avoids edge cases when streaming narrow types.

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.