Created
July 13, 2017 14:52
-
-
Save madsunrise/b9b6a4c62f0c8fa903dae50fd6a0cd8d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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