str 2 hex

 1 void str2hex(const char*p,unsigned int index)
 2 {
 3       unsigned char i=0,j=0;
 4       for(;i < index;)
 5       {
 6           hexBuf[j]<<=4;
 7           if(*p >= '0' && '9' >= *p)
 8           {
 9               hexBuf[j]|=*p++ - '0';
10           }
11           else if(*p >='a' && 'f'>=*p)
12           {
13               hexBuf[j]|=*p++ - 87;
14           }
15           else if(*p >='A' && 'F'>=*p)
16           {
17               hexBuf[j]|=*p++ - 55;
18           }
19           else
20           {
21               return;             //invalid package received
22           }
23           i++;
24           if(!(i%2))
25           {
26               j++;
27           }
28       }
29 }
原文地址:https://www.cnblogs.com/lumao1122-Milolu/p/15015930.html