#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned char StrTohex::strTohex(unsigned char* p_str, unsigned int str_len, unsigned char* p_data, unsigned int datlen)
{
unsigned int i = 0;
if ((!p_data) || (!p_str) || (!str_len) || (2 * datlen < str_len))
return false;
for (i = 0; i < str_len; i += 2) {
ascToByte(p_str, p_data++);
p_str += 2;
}
return true;
}
unsigned char StrTohex::ascToByte(unsigned char* p_str, unsigned char* data)
{
unsigned char tmphexdata = 0;
if (!p_str) return false;
if (!data) return false;
tmphexdata = ascTohex(*p_str++);
tmphexdata <<= 4;
tmphexdata |= ascTohex(*p_str++);
*data = tmphexdata;
return true;
}
unsigned char StrTohex::ascTohex(unsigned char asc)
{
switch (asc) {
case '0': return 0x0;
case '1': return 0x1;
case '2': return 0x2;
case '3': return 0x3;
case '4': return 0x4;
case '5': return 0x5;
case '6': return 0x6;
case '7': return 0x7;
case '8': return 0x8;
case '9': return 0x9;
case 'a':
case 'A': return 0xa;
case 'b':
case 'B': return 0xb;
case 'c':
case 'C': return 0xc;
case 'd':
case 'D': return 0xd;
case 'e':
case 'E': return 0xe;
case 'f':
case 'F': return 0xf;
default : return 0xff;
}
}
字符串转16进制
于 2023-03-30 14:09:30 首次发布