1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > C语言字符串与整形互转

C语言字符串与整形互转

时间:2020-11-04 01:21:47

相关推荐

C语言字符串与整形互转

一、字符串转换为十六进制的整形:

int hex_string_to_u8(const char *hex_str, uint8_t *out){if(strlen(hex_str) & 1){//长度是否为奇数printf("hex_str is error!\n");return -1;}char byte[3] = {0};const char *p = hex_str;int j = 0;for(int i=0; i<strlen(hex_str); i+=2){memcpy(byte, &p[i], 2);out[j++] = strtol(byte, NULL, 16);} return 0;}

二、十六进制的整形转换为字符串:

int arrayToStr(uint8_t *buf, uint8_t buflen, char *out){char strBuf[400] = {0};char pbuf[400];int a,i;a=0;memset(pbuf,0,400);for(i = 0; i < buflen; i++){a+= sprintf(pbuf+a, "%02X", buf[i]);}strncpy(out, pbuf, buflen*2);out[buflen*2] = '\0';//return buflen * 2;}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。