废话不多说,直接上代码
#include <iostream>
#include <Windows.h>
using namespace std;
int hexCharToInt(char c)
{
if (c >= '0' && c <= '9') return (c - '0');
if (c >= 'A' && c <= 'F') return (c - 'A' + 10);
if (c >= 'a' && c <= 'f') return (c - 'a' + 10);
return 0;
}
void cstr_to_ushort(char* cstr, USHORT& str_ushort)
{
for (int i = 0; i < 4; i++) {
char tmp;
strncpy(&tmp, &cstr[i], 1);
cout << "tmp = " << tmp << endl;
int ai = hexCharToInt(tmp);
str_ushort = str_ushort | ai << (12 - 4 * i);
cout << str_ushort << endl;
}
}
int main()
{
char a[5] = "04F3";
USHORT us = 0;
cstr_to_ushort(a, us);
printf("%x", us);
int res = getchar();
return 0;
}
|