• 数据转换考试qwq


    数据转换-位串字节数组

    utils.c:

    #include<stdio.h>
    #include<string.h>
    #include"utils.h"
    
    int Hex2Char(int fromi,char *toc)
    {
        if(fromi>=0&&fromi<=9)
        {
                *toc= fromi+'0';
        }
        else if(fromi>=10&&fromi<=15)
        {
                *toc = fromi+'A'-10;
        }
        else
        {
            printf("error");
         }
        return 0;
    }
    int Char2Hex(char fromc,int *toi)
    {
            if(fromc>='0'&& fromc<='9')
            {
                 *toi= fromc-'0';
            }
            else if(fromc>='A'&& fromc<='F')
            {
                 *toi= fromc-'A'+10;
    
            }
            else
            {
                printf("error");
            }
            return 0;
    }
    int ByteArr2BitStr(char *ba,char *bs)
    {
        int i,j,L,k,a;
        L = strlen(ba);
        for(j=0;j<L;j++)
        {
            Char2Hex(ba[j],&a);
            for(i=0;i<4;i++)
            {
                bs[3*(j+1)-i+j] = a%2+'0';
                a=a/2;
            }
        }
        bs[4*L]='';
    }
    int BitStr2ByteArr(char *bs,char *ba){
       int i,k,j,L;
       L = strlen(bs);
       char string[100];int sum=0;
       if(L%4==1)
       {
         strcpy(string,"000");
         strcat(string,bs);
         k=L/4+1;
       }
       else if(L%4==2)
       {
         strcpy(string,"00");
         strcat(string,bs);
         k=L/4+1;
       }
        else if(L%4==3)
       {
          strcpy(string,"0");;
          strcat(string,bs);
          k=L/4+1;
       }
       else
       {
           k=L/4;
           strcpy(string,bs);
       }
       for(i=0;i<k;i++)
       {
           for(j=0;j<4;j++)
           {
               sum=sum*2;
               sum=sum+(string[4*i+j]-'0');
           }
           Hex2Char(sum,&ba[i]);
           sum=0;
       }
       ba[i]='';
    }

    main.c

    #include<stdio.h>
    #include"utils.h"
    int main(){
      char ba[100];
      char bs[100];
      printf("input ByteArr:
    ");
      scanf("%s",ba);
      ByteArr2BitStr(ba,bs);
      printf("bitstr:
    ");
      printf("%s
    ",bs);
    
      char baa[100];
      char bss[100];
      printf("input bitstr:
    ");
      scanf("%s",bss);
      BitStr2ByteArr(bss,baa);
      printf("ByteArr:
    ");
      printf("%s
    ",baa);
      return 0;
    }

    utils.h

    #ifndef _UTIL_H_
    #define _UTIL_H_
    
    //char Hex2Char(int i);
    //int Char2Hex(char c);
    
    int Hex2Char(int fromi,char * toc);
    int Char2Hex(char fromc,int * toi);
    
    int Bitstr2ByteArr(char * bs,char * ba);
    int ByteArr2Bitstr(char * ba,char * bs);
    
    int Int2ByteArr(int i,char * ba);
    int ByteArr2Int(char * ba,int * i);
    
    #endif

     test.c

    #include<stdio.h>
    #include<string.h>
    //十六进制字符串转二进制字符串
    void Hex2Bin(char *source,char *dest,int len)
    {
        int i=0;
        char Dict[17][5] =
        {
            "0000", "0001", "0010", "0011",
            "0100", "0101", "0110", "0111",
            "1000", "1001", "1010", "1011",
            "1100", "1101", "1110", "1111",
        };
        for(i=0;i<len;i++){
            //char temp[5]={0};
            int n = 16;
            if (source[i] >= 'a' && source[i] <= 'f') n = source[i] - 'a' + 10;
            if (source[i] >= 'A' && source[i] <= 'F') n = source[i] - 'A' + 10;
            if (source[i] >= '0' && source[i] <= '9') n = source[i] - '0';
             //sprintf(temp,"%s", Dict[n]);
             //memcpy(&dest[i*4],temp,4);
             memcpy(&dest[i*4],Dict[n],4);
        }
        return;
    }
    
    int main()
    {
        char Hexstring[64]="94a8";
        char Binarystring[256]={0};
        Hex2Bin(Hexstring,Binarystring,strlen(Hexstring));
        printf("十六进制串长度:%d十六进制串:%s,
    二进制串长度:%d,二进制串:",strlen(Hexstring),Hexstring,strlen(Binarystring));
        puts(Binarystring);
        return 0;
    }
  • 相关阅读:
    在VMware中使用Nat方式设置静态IP
    saltstack实现自动化扩容
    saltstack常用模块
    saltstack之nginx、php的配置
    桶排序
    【前端安全】JavaScript防http劫持与XSS
    memcached
    10 行 Python 代码写的模糊查询
    为什么print在python3中变成了函数?
    一行python代码实现树结构
  • 原文地址:https://www.cnblogs.com/liang20181208/p/14763218.html
Copyright © 2020-2023  润新知