Last active
January 23, 2021 13:10
-
-
Save Maverick-I0/722c9ca609f859c86df1203d58522ea8 to your computer and use it in GitHub Desktop.
A simple function to converts characters to its ASCII hex value. The function takes in the character and returns the ASCII HEX value for the character as string.
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
| static string char2Hex(unsigned char _char) { | |
| string hex; | |
| int ascii_int = int(_char); | |
| char hex_array[20]; | |
| int i = 0; | |
| while (ascii_int != 0) { | |
| int temp = 0; | |
| temp = ascii_int % 16; | |
| if (temp < 10) { | |
| hex_array[i] = temp + 48; | |
| i++; | |
| } else { | |
| hex_array[i] = temp + 55; | |
| i++; | |
| } | |
| ascii_int = ascii_int / 16; | |
| } | |
| string ans; | |
| for (int j = i - 1; j >= 0; j--) | |
| hex += hex_array[j]; | |
| return hex; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment