欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

16进制与字符串相互转换(C语言)

程序员文章站 2022-03-14 15:55:44
...

编译语言: C
编译工具链:MinGW GCC
编译环境:eclipse


字符串转换成16进制函数
先写一个字符转大写的函数lowtocap
(也可以直接调用C库函数toupper,需#include <ctype.h>)

/*lowercase letters transform to capital letter*/
char lowtocap(char c)
{
       if((c >= 'a') && (c <= 'z'))
       {
              c = c - 32;       // 或者c = c + ('a' - 'A');
       }
       return c;
}

/*ascii string transform to 16 jinzhi Hex*/
void AsciiToHex(char *src, uint8_t *dest, int len)
{
    int dh,dl;   // 16进制的高4位和低4位
    char ch,cl;  //字符串的高位和低位
    int i;
    if(src == NULL || dest == NULL)
    {
       printf("src or dest is NULL\n");
       return;
    }
    if(len < 1)
    {
       printf("length is NULL\n");
       return;
    }
    for(i = 0; i < len; i++)
    {
       ch = src[2*i];
       cl = src[2*i+1];
       dh = lowtocap(ch) - '0';
       if(dh > 9)
       {
              dh = lowtocap(ch) - 'A' + 10;   //lowtocap可以用库函数toupper
       }
       dl = lowtocap(cl) - '0';
       if(dl > 9)
       {
              dl = lowtocap(cl) - 'A' + 10;   //lowtocap可以用库函数toupper
       }
       dest[i] = dh * 16 + dl;
    }
    if(len%2 > 0)   //字符串个数为奇数
    {
       dest[len/2] = src[len-1] - '0';
       if(dest[len/2] > 9)
       {
              dest[len/2] = lowtocap(src[len-1]) - 'A' + 10;
       }
    }
}

16进制转换成字符串函数

/*16 jinzhi Hex transform to ascii string*/
void HexToAscii(uint8_t *src, char *dest, int len)
{
       char dh,dl;  //字符串的高位和低位
       int i;
       if(src == NULL || dest == NULL)
       {
              printf("src or dest is NULL\n");
              return;
       }
       if(len < 1)
       {
              printf("length is NULL\n");
              return;
       }
       for(i = 0; i < len; i++)
       {
              dh = '0' + src[i] / 16;
              dl = '0' + src[i] % 16;
              if(dh > '9')
              {
                     dh = dh - '9' - 1 + 'A'; // 或者 dh= dh+ 7;
              }
              if(dl > '9')
              {
                     dl = dl - '9' - 1 + 'A'; // 或者dl = dl + 7;
              }
              dest[2*i] = dh;
              dest[2*i+1] = dl;
       }
       dest[2*i] = '\0';
}

开始测试

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

char *c1 = "ABCEEF12345678900C0B";    //待测试字符串
uint8_t s[7] = {0xAB,0xCC,100,50,10,9,8};  //待测试16进制数组

int main(void) {
    int i,len;
    uint8_t temp[30] = {0};
    char temp1[50] = {0};

    AsciiToHex(c1,temp, strlen(c1));
    if(strlen(c1)%2 > 0)
    {
       len = strlen(c1)/2 + 1;
    }
    else
    {
       len = strlen(c1)/2;
    }
    for( i = 0; i < len; i++)
    {
       printf("int[%d] is %X,%d\n", i+1,temp[i],temp[i]);
    }
    
    HexToAscii(s,temp1,sizeof(s));
    printf("temp1 is %s", temp1);
    return EXIT_SUCCESS;
}

测试结果如下:

int[1] is AB,171
int[2] is CE,206
int[3] is EF,239
int[4] is 12,18
int[5] is 34,52
int[6] is 56,86
int[7] is 78,120
int[8] is 90,144
int[9] is C,12
int[10] is B,11
temp1 is ABCC64320A0908