#pragma once
#include <iostream>
#include <iomanip> //std::hex, std::setw, std::setfill
template <class T>
T str2int(string intstr) {
istringstream intstream(intstr);
T val;
intstream >> val;
return val;
}
template <class T>
string int2string(T val, string format) {
ostringstream hexstream;
int size = str2int<int>(format.substr(1, format.length() - 1));
string str = format.substr(0, 1);
if (str == "D" || str == "d") {
if (format.substr(1, 1) == "0") hexstream << setw(size) << setfill('0') << val;
else hexstream << setw(size) << val;
}
else if (str == "X") {
hexstream << hex << setw(size) << setfill('0') << setiosflags(ios::uppercase) << val;
}
else if (str == "x") {
hexstream << hex << setw(size) << setfill('0') << val;
}
return hexstream.str();
}
template <class T>
string int2str(T val) {
ostringstream strstream;
strstream << val;
return strstream.str();
}
|