//
//eg1:hexToAscii
//
#include <stdio.h>
#include <stdlib.h>
/*
* To convert 53 to the character 'S':
* char returnVal = hexToString('5', '3');
*/
char hexToAscii(char first, char second)
{
char hex[5], *stop;
hex[0] = '0';
hex[1] = 'x';
hex[2] = first;
hex[3] = second;
hex[4] = 0;
return strtol(hex, &stop, 16);
}
int main(int argc, char* argv[])
{
printf("%c/n", hexToAscii('5', '3'));
}
//
//eg2:AsciitoHex in C
//
#include <stdio.h>
void text_to_hex(char *is_ascii_string, char *os_hex_string, int ii_ascii_string_size);
int main()
{
FILE *pf_in_file = NULL;
char ps_in_filename[20] = "";
int pi_input_chunk_size = 0;
char ps_input_chunk[161] = "";
long pl_bytes_to_read = 160;
char ls_input_chunk_raw[220] = "";
strcpy(ps_input_chunk, "<8f><80>âY^SZÃpS ðï^Y");
pi_input_chunk_size = strlen(ps_input_chunk);
printf("INput chunk = [%s]/n", ps_input_chunk);
printf("Read buf size = [%d]/n", pi_input_chunk_size);
text_to_hex(ps_input_chunk, ls_input_chunk_raw, pi_input_chunk_size);
printf("ls_input_chunk_raw = [%s]/n", ls_input_chunk_raw);
return (0);
}
void text_to_hex(char *is_ascii_string, char *os_hex_string, int ii_ascii_string_size)
{
char *function = "text_to_hex";
char ls_hex[3] = "";
int i = 0;
int j = 0;
memset(ls_hex, '/0', 3);
j = 0;
for (i=0; i < ii_ascii_string_size; i++)
{
sprintf(ls_hex, "%.2X", is_ascii_string[i]);
os_hex_string[j++] = ls_hex[0];
os_hex_string[j++] = ls_hex[1];
}
os_hex_string[j] = '/0';
}
///
//eg3.Convert the Ascii to Hex in C
///
int main()
{
char symbols [] = "0123456789abcdef";
char result [4];
unsigned i, j, lsd, hexNum;
for (i = 0; i < 256; ++i)
{
hexNum = i;
j = 0;
strcpy (result, "0");
do
{
lsd = hexNum % 16;
result [j++] = symbols [lsd];
hexNum /= 16;
} while (hexNum);
result [j] = '/0';
printf ("Hex: %s/n", _strrev (result));
}
return (0);
}
///
//eg4.
///
cout<<hex<<(int)'tian'<<endl;//貌似这样也行。。
///
//eg5. Transform a word into ASCII code
///
#include <iostream>
using namespace std;
int main()
{
char word[32];
int x = 0;
cout << "Please enter the word (maximum 32 characters):/n";
cin >> word;
cout << "The ASCII for this word is:/n";
while (word[x] != '/0') // While the string isn't at the end...
{
cout << int(word[x]); // Transform the char to int
x++;
}
cout << "/n";
return 0;
}
///
//eg6.
///
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
int main()
{
string val("54657374");
stringstream ss(val);
long l;
ss >hex >l;
cout << "val ='" << val << "', l = " << l << ", hex(l) ='" << hex << l << "'"<< endl;
return 0;
}
///
//eg6. If you want to convert hex string into a number use strtol()
///
#include <cstdlib>
#include <cstdio>
using namespace std;
int main(int argc, char **argv)
{
if (argc==1)
{
fptus("need an argument/n", stderr);
return 1;
}
while (--argc)
{
char *end;
long num = strtol(*++argv, &end, 16);
if (*end)
{
/* you could also add errno checking */
fprintf(stderr, "%s: invalid number/n", *argv);
}
else
{
printf("%d/n", num);
}
}
return 0;
}
///
//eg7. One way is using stringstreams:
///
#include <iostream>
#include <cstdlib>
#include <string>
#include <sstream>
std::string hex2DecString(std::string hexStr)
{
std::stringstream ss;
// Read hexadecimal number into stream
if (!(ss << std::hex << hexStr))
{
throw "hex2DecString: Conversion 1 error.";
}
// Read integer from stream
int intVal;
if (!(ss >intVal))
{
throw "hex2DecString: Conversion 2 error.";
}
// Read integer back into stream as decimal
ss.clear(); // **See note below**
ss.str("");
if (!(ss << std::dec << intVal))
{
throw "hex2DecString: Conversion 3 error.";
}
// Extract decimal string of number
return ss.str();
}
int main()
{
std::string hexStr = "1AB";
std::string decStr = hex2DecString(hexStr);
std::cout << "Hex = " << hexStr << "/tDecimal = " << decStr << std::endl;
return EXIT_SUCCESS;
}
Table of ASCII Characters
http://web.cs.mun.ca/~michael/c/ascii-table.html