#pragma once
#include <iostream>
#include <iomanip> //std::hex, std::setw, std::setfill
template <class T>
T hex2int(string hexstr) {
istringstream intstream(hexstr);
T val;
intstream >> hex >> val;
return val;
}
template <class T>
string int2hex(T val, int size) {
ostringstream hexstream;
hexstream << hex << setw(size) << setfill('0') << setiosflags(ios::uppercase) << val;
return hexstream.str();
}
uint64_t hex2int64(string hexstr) {
uint64_t val = 0;
if (hexstr.length() > 8) {
val = hex2int<uint32_t>(hexstr.substr(0, hexstr.length() - 8));
val <<= 32;
val |= hex2int<uint32_t>(hexstr.substr(hexstr.length() - 8, 8));
}
else {
val = hex2int<uint32_t>(hexstr);
}
return val;
}
string int2hex64(uint64_t val) {
return int2hex<uint32_t>(val >> 32, 8) + int2hex<uint32_t>(val & 0xffffffff, 8);
}
|