Skip to content

Instantly share code, notes, and snippets.

@madsunrise
Created July 13, 2017 14:52
Show Gist options
  • Save madsunrise/b9b6a4c62f0c8fa903dae50fd6a0cd8d to your computer and use it in GitHub Desktop.
Save madsunrise/b9b6a4c62f0c8fa903dae50fd6a0cd8d to your computer and use it in GitHub Desktop.
std::wstring toFormattedHex(int input) {
const int max_int_length = 10;
wchar_t* temp = new wchar_t[max_int_length + 1];
swprintf( temp, max_int_length, L"%x", input);
std::wstring hex(temp);
delete [] temp;
std::wstring result = L"00 00";
switch (hex.length()) {
case 1: {
result.replace(1, 1, hex.substr(0, 1));
break;
}
case 2: {
result.replace(0, 2, hex.substr(0, 2));
break;
}
case 3: {
result.replace(0, 2, hex.substr(1, 2));
result.replace(3, 1, hex.substr(0, 1));
break;
}
case 4: {
result.replace(0, 2, hex.substr(2, 2));
result.replace(3, 2, hex.substr(0, 2));
break;
}
default:
break;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment