16进制转字符串&&字符串转16进制

//16进制转字符串

void HexToStr(char *pbDest, char *pbSrc, int nLen)
{
    unsigned char ddl,ddh;
    int i;

    for (i=0; i<nLen; i++)
    {
        ddh = 48 + (unsigned char)pbSrc[i] / 16;
        ddl = 48 + (unsigned char)pbSrc[i] % 16;
        if (ddh > 57) ddh = ddh + 7;
        if (ddl > 57) ddl = ddl + 7;
        pbDest[i*2] = ddh;
        pbDest[i*2+1] = ddl;
    }

    //pbDest[nLen*2] = '\0';
}


//字符串转16进制
void StrToHex(char *pbDest, char *pbSrc, int nLen)
{
    unsigned char h1,h2;
    unsigned char s1,s2;
    int i;

    for (i=0; i<nLen; i++)
    {
        h1 = pbSrc[2*i];
        h2 = pbSrc[2*i+1];

        s1 = toupper(h1) - 0x30;
        if (s1 > 9) 
            s1 -= 7;

        s2 = toupper(h2) - 0x30;
        if (s2 > 9) 
            s2 -= 7;

        pbDest[i] = s1*16 + s2;
    }
}

 

一、将数组转换为十六进制同值的字符串

   读取数组中的数字,打印成字符串的时候以2位大写的格式。

int arrayToStr(unsigned char *buf, unsigned int buflen, char *out)
{
    char strBuf[33] = {0};
    char pbuf[32];
    int i;
    for(i = 0; i < buflen; i++)
    {
        sprintf(pbuf, "%02X", buf[i]);
        strncat(strBuf, pbuf, 2);
    }
    strncpy(out, strBuf, buflen * 2);
    printf("out = %s\n", out);
    return buflen * 2;
}
二、将十六进制的字符串转换为十六进制数组

下面定义的字符串中的字符只能是0-F的字符,但是不区分大小写的,前面是安装两位为一个数字进行转换,最后一个数字如果还是两位的则正常转换,如果只剩一位的话则在前面补零输出。

int StringToHex(char *str, unsigned char *out, unsigned int *outlen)
{
    char *p = str;
    char high = 0, low = 0;
    int tmplen = strlen(p), cnt = 0;
    tmplen = strlen(p);
    while(cnt < (tmplen / 2))
    {
        high = ((*p > '9') && ((*p <= 'F') || (*p <= 'f'))) ? *p - 48 - 7 : *p - 48;
        low = (*(++ p) > '9' && ((*p <= 'F') || (*p <= 'f'))) ? *(p) - 48 - 7 : *(p) - 48;
        out[cnt] = ((high & 0x0f) << 4 | (low & 0x0f));
        p ++;
        cnt ++;
    }
    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 StringToCom(char *str, unsigned char *out, int *outlen)
{
    char *p = str;
    char high = 0, low = 0;
    int tmplen = strlen(p), cnt = 0;
    tmplen = strlen(p);
    if(tmplen % 2 != 0) return -1;
    while(cnt < tmplen / 2) //1213141516171819
    {
        out[cnt] = (*p - 0x30) * 10 + (*(++p) - 0x30);
        p++;
        cnt ++;
    }
    *outlen = tmplen / 2;
    return tmplen / 2;
}
四、简单的使用方法

定义的参数有些为unsigned char,是因为在定义为char的时候,转换为十六进制之后,负数在表示的时候,难看!

#include "stdio.h"
#include "stdlib.h"
#include "string.h"
 
unsigned char ArrayCom[16] = {
    11, 12, 13, 14, 15, 16, 17, 18,
    19, 20, 21, 22, 23, 24, 25, 26};
unsigned char ArrayHex[16] = {
    0x2c, 0x57, 0x8f, 0x79, 0x27, 0xa9, 0x49, 0xd3,
    0xb5, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
 
char *strHex = "01aa0304050607083f0add0c0d0e0f00";
char *strCom = "1D1213AB6FC1718B19202122232425A6";
 
int main(int argc, const char *argv)
{
    int cnt;
    char str[33] = {0};
    unsigned char out[33];
    arrayToStr(ArrayCom, 16, str);
    
    int outlen = 0;
    StringToHex(strCom, out, &outlen);
    for(cnt = 0; cnt < outlen; cnt ++)
    {
        printf("%02X ", out[cnt]);
    } 
    putchar(10);
 
    return 0;
}
 

原文链接:https://blog.csdn.net/zhemingbuhao/article/details/83111564

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值