本文共 1870 字,大约阅读时间需要 6 分钟。
#include#include typedef unsigned char BOOL_T; typedef unsigned char U8_T; typedef signed char S8_T; typedef unsigned short U16_T; typedef signed short S16_T; typedef unsigned int U32_T; typedef signed int S32_T;typedef unsigned __int64 U64_T;typedef signed __int64 S64_T; typedef float F32_T; typedef double F64_T; typedef char * STR_T;//将数组转换为十六进制同值的字符串void arrayToStr(U8_T *buf, unsigned int buflen, U8_T *out){ U8_T strBuf[33] = {0}; U8_T pbuf[32]; U8_T i; for(i = 0; i < buflen; i++) { sprintf(pbuf, "%02X", buf[i]); strncat(strBuf, pbuf, 2); } strncpy(out, strBuf, buflen * 2); out[buflen*2] = 0;}int main(){ U8_T str[25] = {0x33,0x01,0x08,0x00,0x17,0x90,0x59,0x26}; U8_T *ptr; arrayToStr(str, 8, ptr); printf("结果:%s\n", ptr); return(0);}
运行结果:
封装,用函数调用(运行结果一样)
#include#include typedef unsigned char BOOL_T; typedef unsigned char U8_T; typedef signed char S8_T; typedef unsigned short U16_T; typedef signed short S16_T; typedef unsigned int U32_T; typedef signed int S32_T;typedef unsigned __int64 U64_T;typedef signed __int64 S64_T; typedef float F32_T; typedef double F64_T; typedef char * STR_T;U8_T arrayToStr(U8_T *buf, U8_T buflen, U8_T *out){ U8_T strBuf[33] = {0}; U8_T pbuf[32]; U8_T i; for (i = 0; i < buflen; i++) { sprintf((char*)pbuf, "%02X", buf[i]); strncat((char*)strBuf, (char*)pbuf, 2); } strncpy((char*)out, (char*)strBuf, buflen * 2); out[buflen*2] = 0; return (buflen * 2);}//封装调用函数 void fun(){ U8_T str[25] = {0x33,0x01,0x08,0x00,0x17,0x90,0x59,0x26}; char ptr[33] = {0};//这个变量不能定义成指针,是指针会导致奔溃 //将数组转换为十六进制同值的字符串 arrayToStr(str, 8, ptr); printf("结果:%s\n", ptr); }int main(){ fun(); return(0);}
转载地址:http://bopcz.baihongyu.com/