一、
自己收藏的几个函数,方便使用
使用时注意验证一下
(1)字符串转16进制
//输出的16进制字符中开头不加0x
//str----带转化字符串缓存区
//str_len---字符串长度
//out_str---16进制存储缓冲区
//out_len---16进制缓存区长度(必须大于字符串长度的2倍)
int convert_str_to_hex(unsigned char *str, unsigned int str_len, unsigned char *out_str, unsigned int *out_len)
{
char i = 0 ;
unsigned char ch1 = 0, ch2 = 0;
if(str_len * 2 > *out_len)
{
return -1;
}
for(i = 0; i < str_len; i++)
{
ch1 = (str[i] & 0xf0) >> 4;
ch2 = (str[i] & 0x0f);
ch1 += ((ch1 > 9) ? 0x57 : 0x30);
ch2 += ((ch2 > 9) ? 0x57 : 0x30);
out_str[2*i + 0] = ch1;
out_str[2*i + 1] = ch2;
}
out_str[str_len * 2 ] = 0;
*out_len = str_len * 2;
printf("out_str:%s\n",out_str);
return 0;
}
//输出的16进制字符中开头加0x
//str----带转化字符串缓存区
//str_len---字符串长度
//out_str---16进制存储缓冲区
//out_len---16进制缓存区长度(必须大于字符串长度的2倍)
int convert_str_to_hex(uint8_t *str, int32_t str_len, uint8_t *out_str, int32_t *out_len)
{
uint32_t i = 0 ;
uint8_t ch1 = 0, ch2 = 0;
if(str_len * 2 + 2 > *out_len)
{
return -1;
}
sprintf(out_str, "0x");
for(i = 0; i < str_len; i++)
{
ch1 = (str[i] & 0xf0) >> 4;
ch2 = (str[i] & 0x0f);
ch1 += ((ch1 > 9) ? 0x57 : 0x30);
ch2 += ((ch2 > 9) ? 0x57 : 0x30);
out_str[2*i + 2] = ch1;
out_str[2*i + 3] = ch2;
}
out_str[str_len * 2 + 2] = 0;
*out_len = str_len * 2 + 2;
return 0;
}
(2)16进制转字符串
//16进制转化成字符串
int convert_hex_to_str(uint8_t *src, uint32_t len, uint8_t *dest)
{
uint32_t i = 0 ;
uint8_t ch1, ch2;
if(len % 2 != 0)
{
printf("%s,%d - in convert_hex_to_str(), len:%u % 2 != 0!\n", __FILE__, __LINE__,len);
return -1;
}
for(i = 0; i < len/2; i++)
{
ch1 = src[i*2];
ch2 = src[i*2+1];
ch1 -= ((ch1 > (9+0x30)) ? 0x57 : 0x30);
ch2 -= ((ch2 > (9+0x30)) ? 0x57 : 0x30);
dest[i] = (ch1<<4)|ch2;
}
return 0;
}
二、
(1)字符串转16进制
/**
*****************************************************************************
* @Name : 字符串转换成十六进制数据组
*
* @Brief : none
*
* @Input : *str: 字符串输入
* buflen: 十六进制数组长度
*
* @Output : *out: 十六进制数组输出缓冲区
* *outlen: 十六进制数组长度
*
* @Return : 十六进制数组长度
*****************************************************************************
**/
int StringToHex(char *str, unsigned char *out, unsigned int *outlen)
{
char *p = str;
char high = 0, low = 0;
int tmplen = strlen(p), cnt = 0;
if (str == NULL || out == NULL)
{
return 0;
}
//5e16000100086735204061430404010411110086fd5e
tmplen = strlen(p);
while(cnt < (tmplen / 2))
{
if (((*p >= '0') && (*p <= '9')) ||\
((*p >= 'A') && (*p <= 'F')) ||\
((*p >= 'a') && (*p <= 'f')))
{
high = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 : *p - 48;
p++;
}
else
{
*outlen = 0;
return 0;
}
if (((*p >= '0') && (*p <= '9')) ||\
((*p >= 'A') && (*p <= 'F')) ||\
((*p >= 'a') && (*p <= 'f')))
{
low = (*(p) > '9' && ((*p <= 'F') || (*p <= 'f'))) ? *(p) - 48 - 7 : *(p) - 48;
out[cnt] = ((high & 0x0f) << 4 | (low & 0x0f));
p ++;
cnt ++;
}
else
{
*outlen = 0;
return 0;
}
}
if(tmplen % 2 != 0) out[cnt] = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 : *p - 48;
if(outlen != NULL) *outlen = tmplen / 2 + tmplen % 2;
return tmplen / 2 + tmplen % 2;
}
(2)16进制转字符串
/**
*****************************************************************************
* @Name : 8bits十六进制转换成字符串
*
* @Brief : none
*
* @Input : hex: 十六进制数
*
* @Output : *strout: 字符串输出缓冲区
*
* @Return : 字符串输出指针
*****************************************************************************
**/
char *hex2str(const char hex, char *strout)
{
char val = hex;
char hh = (val & 0xF0) >> 4, ll = val & 0x0F;
*strout++ = hh < 0xA ? hh + '0' : hh + '7';
*strout++ = ll < 0xA ? ll + '0' : ll + '7';
return strout;
}
/**
*****************************************************************************
* @Name : 十六进制数据组转换成字符串
*
* @Brief : none
*
* @Input : *pBufHex: 十六进制数组
* buflen: 十六进制数组长度
*
* @Output : *strout: 字符串输出缓冲区
*
* @Return : 字符串长度
*****************************************************************************
**/
int arrayToStr(const char *pBufHex, unsigned int buflen, char *out)
{
// char strBuf[512] = {0};
char pbuf[2];
int i;
for(i = 0; i < buflen; i++)
{
// sprintf(pbuf, "%02X", pBufHex[i]);
hex2str(*pBufHex++, (char *)pbuf);
// strncat(strBuf, pbuf, 2);
strncat(out, pbuf, 2);
}
// strncpy(out, strBuf, buflen * 2);
// printf("out = %s\n", out);
return buflen * 2;
}
三、测试
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
/**
*****************************************************************************
* @Name : 字符串转换成十六进制数据组
*
* @Brief : none
*
* @Input : *str: 字符串输入
* buflen: 十六进制数组长度
*
* @Output : *out: 十六进制数组输出缓冲区
* *outlen: 十六进制数组长度
*
* @Return : 十六进制数组长度
*****************************************************************************
**/
int StringToHex(char *str, unsigned char *out, unsigned int *outlen)
{
char *p = str;
char high = 0, low = 0;
int tmplen = strlen(p), cnt = 0;
if (str == NULL || out == NULL)
{
return 0;
}
//5e16000100086735204061430404010411110086fd5e
tmplen = strlen(p);
while(cnt < (tmplen / 2))
{
if (((*p >= '0') && (*p <= '9')) ||\
((*p >= 'A') && (*p <= 'F')) ||\
((*p >= 'a') && (*p <= 'f')))
{
high = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 : *p - 48;
p++;
}
else
{
*outlen = 0;
return 0;
}
if (((*p >= '0') && (*p <= '9')) ||\
((*p >= 'A') && (*p <= 'F')) ||\
((*p >= 'a') && (*p <= 'f')))
{
low = (*(p) > '9' && ((*p <= 'F') || (*p <= 'f'))) ? *(p) - 48 - 7 : *(p) - 48;
out[cnt] = ((high & 0x0f) << 4 | (low & 0x0f));
p ++;
cnt ++;
}
else
{
*outlen = 0;
return 0;
}
}
if(tmplen % 2 != 0) out[cnt] = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 : *p - 48;
if(outlen != NULL) *outlen = tmplen / 2 + tmplen % 2;
return tmplen / 2 + tmplen % 2;
}
int convert_str_to_hex(unsigned char *str, unsigned int str_len, unsigned char *out_str, unsigned int *out_len)
{
char i = 0 ;
unsigned char ch1 = 0, ch2 = 0;
if(str_len * 2 > *out_len)
{
return -1;
}
for(i = 0; i < str_len; i++)
{
ch1 = (str[i] & 0xf0) >> 4;
ch2 = (str[i] & 0x0f);
ch1 += ((ch1 > 9) ? 0x57 : 0x30);
ch2 += ((ch2 > 9) ? 0x57 : 0x30);
out_str[2*i + 0] = ch1;
out_str[2*i + 1] = ch2;
}
out_str[str_len * 2 ] = 0;
*out_len = str_len * 2;
printf("out_str:%s\n",out_str);
return 0;
}
int main(void)
{
char buf[16]="hello world";
unsigned char buff[32]={0};
int retlen=0;
unsigned int blen=50;
retlen = convert_str_to_hex("hello world",11,buff,&blen);
printf("buff:%x\n",buff[0]);
printf("buff:%x\n",buff[1]);
printf("buff:%x\n",buff[2]);
printf("buff:%x\n",buff[3]);
printf("buff:%x\n",buff[4]);
printf("blen:%d\n",blen);
printf("retlen:%d\n",retlen);
return 0;
}