• 2021.5.13课堂测试


    (一)在utils.h和utils.c中完成16进制字符'0'-'9','A'-'F','a'-'f'与十六进制数据0-15的转换功能

    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

    utils.c

    #include<stdio.h>
    #include"util.h"
    char Hstr = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
    char Hex2Char(int i){
    if(i >= 0 && i <= 9){
    return i + 0x30;
    // return i + '0'
    }
    if(i >= 10 && i <= 15){
    return i + 0x37;
    // return i + 'A' - 10
    }
    return 0;
    }
    
    /*
    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;
    }
    
    */ 

    main.c

    #include<stdio.h>
    #include"utils.h"
    int main(){
      int n1,n2;
      char c1,c2;
      printf("请输入一个0~15的数:");
      scanf("%d",&n1);
      Hex2Char(n1,&c1);
      printf("对应的16进制字符是%c
    ",c1);
      printf("请输入一个16进制字符:");
      scanf("%c",&c2);
      Char2Hex(c2,&n2);
      printf("%c对应的数是%d
    ",c2,n2);
    }

    (二)在utils.h和utils.c中完成位串与8位字节串的转换功能

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

    utils1.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]='';
    }

  • 相关阅读:
    js 带表情的评论输入框问题
    js 元素到指定的相对定位的父元素的距离
    html分享QQ,微信,显示分享图片,标题,简介
    网络空间安全0x01志向
    优秀的前端
    float探究
    转载的。。。
    判断有木有环
    居中(纯css方式)
    一个闭包的很好的考题, 闭包+递归
  • 原文地址:https://www.cnblogs.com/ZYX-bk-412/p/14762951.html
Copyright © 2020-2023  润新知