Skip to content

Instantly share code, notes, and snippets.

@Maverick-I0
Last active January 23, 2021 13:10
Show Gist options
  • Select an option

  • Save Maverick-I0/722c9ca609f859c86df1203d58522ea8 to your computer and use it in GitHub Desktop.

Select an option

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.
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