怎样把字符串转化成16进制数组给串口发送啊啊??求大神

例如:char tad[]="534D533133343235363834373233"经过一段16进制转换的函数后输出相对应的“0x53,0x4D,0x53,0x31,0x33,0x34...0x33” 高分报答!!用C语言

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    char tad[]="534D533133343235363834373233";
    unsigned char x;
    char hex[3];
    int len=strlen(tad)/2*2;
    char *p=tad,*endp=tad+len;
    while(p<=endp){
        memcpy(hex,p,2);
        hex[2]='\0';
        sscanf(hex,"%2X",&x);
        printf("%X(%u) ",x,x);
        p+=2;
    }
    return 0;
}

53(83) 4D(77) 53(83) 31(49) 33(51) 34(52) 32(50) 35(53) 36(54) 38(56) 34(52) 37(55) 32(50) 33(51) 33(51)
Process returned 0 (0x0)   execution time : 0.031 s
Press any key to continue.

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-05-16
不难吧

有这样的函数吧 atoi ->ascii to int?

C语言库函数名: atoi
功 能: 把字符串转换成整型数。
名字来源:ASCII to integer 的缩写。
原型: int atoi(const char *nptr);
函数说明: 参数nptr字符串,如果第一个非空格字符存在,并且,如果不是数字也不是正负号则返回零,否则开始做类型转换,之后检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数。
第2个回答  2013-05-16
字符串本来就是16进制数组嘛,直接送不就行了。为何 要变成要“0x53”,
这不白白增加了长度了,除非发送协议中这么规定
相似回答