Skip to content

Instantly share code, notes, and snippets.

@karoltheguy
Last active August 29, 2015 14:02
Show Gist options
  • Save karoltheguy/4c5c6540c03b7e7c9a42 to your computer and use it in GitHub Desktop.
Save karoltheguy/4c5c6540c03b7e7c9a42 to your computer and use it in GitHub Desktop.
Hexadecimal to ASCII in C++
#include <iostream>
#include <stdio.h>
using namespace std;
int hexToInt(char c)
{
int first = c / 16 - 3;
int second = c % 16;
int result = first*10 + second;
if (result > 9) result--;
return result;
}
int hexToLetter(char c, char d)
{
int high = hexToInt(c) * 16;
int low = hexToInt(d);
return high+low;
}
int main()
{
const char* hexChar = "0008706574756C61";
int length = strlen(hexChar);
char buf = 0;
cout << "FROM HEX: \n\r";
for(int i = 0; i < length; i++) {
if(i % 2 != 0) {
printf("%c", hexToLetter(buf,hexChar[i]));
//Different output
//cout << static_cast<char>(hexToLetter(buf,hexChar[i]));
} else {
buf = hexChar[i];
}
}
cout << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment