栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

字符串转16进制 与 16进制转字符串

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

字符串转16进制 与 16进制转字符串

一、
自己收藏的几个函数,方便使用
使用时注意验证一下

(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:%sn",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进制

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进制转字符串

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;
}


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 = %sn", out);
    return buflen * 2;
}

三、测试

#include "stdio.h"
#include "string.h"
#include "stdlib.h"




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:%sn",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:%xn",buff[0]);
	printf("buff:%xn",buff[1]);
	printf("buff:%xn",buff[2]);
	printf("buff:%xn",buff[3]);
	printf("buff:%xn",buff[4]);
	printf("blen:%dn",blen);
	printf("retlen:%dn",retlen);
	
	return 0;
}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/676266.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号