二进制to十六进制

 输入小于等于十六位的二进制数据,输出十六进制数据;

#include 
#include 
#include 






int main(void) {
    char arr[16] = { 0 }; 
    int array[16] = { 0 }; 
    int hex[4] = { 0 };
    int i = 0; 
    int  num = 0;
    scanf("%s", arr);
 
    printf("%s\n", arr);

    while (arr[i] != '\0')
    {
        i++;
    }
    printf("%d\n", i);        //arr[16]={'1','0','0','1','1','0','\0', , , ... , , , }
   
    int len = 16 - i;
    for (int k = i; k >= 0; k--)                 //往后移 arr[16]={'\0', '\0',  ... ,'\0' ,'\0' ,'1','0','0','1','1','0', }
    {                                    
        arr[k + len] = arr[k];
    }                                                           //arr[15-i]

    for (int d = 0; d < len; d++)         //前面的填充为'0'
    {                                            
        arr[d] = '0';
    }
 
    for (int j = 0; j < 16; j++) 
    {
        array[j] = arr[j] - '0';
    }

    for (int s = 0; s < 16; s++)    //array[16]={0,0,0,0,0,0,0,0,1,0,0,1,1,0,0,1}
    {                   
        printf("array[s]=%d\n", array[s]);
    }

    for (int c = 15; c >= 0; c--) 
    {
        int l = 0;
        if (array[c] != 0) 
        {
            l = 15 - c; int x = 0;
            if (l < 4) {
                x = l % 4;
                hex[0] = hex[0] + pow(2, x);
            }
            if (l >= 4 && l < 8) {
                x = (l - 4) % 8;
                hex[1] = hex[1] + pow(2, x);
            }
            if (l >= 8 && l < 12) {
                x = (l - 8) % 12;
                hex[2] = hex[2] + pow(2, x);
            }
            if (l >= 12 && l < 16) {
                x = (l - 12) % 16;
                hex[3] = hex[3] + pow(2, x);
            }
        }
    }

    char ar[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };

    printf("16radix:%c%c%c%c", ar[hex[3]], ar[hex[2]], ar[hex[1]], ar[hex[0]]);
    return 0;
}

生成十六进制的部分有点绕,所以写了个新的,空了补上算法的思路.

#include 
#include 
#include 
 
 
 
 
 
 
int main(void) {
    char arr[16] = { 0 }; 
    int array[16] = { 0 },array_new[16]={0};
    int hex[4] = { 0 };
    int i = 0; 
 
    scanf("%s", arr);
 
    printf("输入为:%s\n", arr);         //假设输入为:arr[16]={'1','1','0','1','1','0','\0', , , ... , , , }
 
    while (arr[i] != '\0')
    {
        i++;
    }
    printf("输入位数=%d\n", i);        
    int len = 16 - i;

    for (int k = i; k >= 0; k--)                 //往后移 arr[16]={'\0', '\0',  ... ,'\0' ,'\0' ,'1','1','0','1','1','0','0' }
    {                                    
        arr[k + len] = arr[k];
    }                                                           //arr[15-i]
 
    for (int d = 0; d < len; d++)         //前面的填充为'0'
    {                                            
        arr[d] = '0';
    }
 
    for (int j = 0; j < 16; j++) 
    {
        array[j] = arr[j] - '0';         //char数字 to int数字; 
    }

  
    for(int i=0;i<16;i++)              //array_new[16]={0,0,1,1,0,1,1,0,0,0,0,0,0,0,0,0}
    {         
    array_new[i]=array[15-i];
    }  

    for(int k=0;k<16;k++)       //倒序生成array_new;
    {
        if((k<4)&&(array_new[k]==1))
        {
            hex[3]=hex[3]+pow(2,k);
        }
        
        if(((k>=4)&&(k<8))&&(array_new[k]==1))
        {
            hex[2]=hex[2]+pow(2,k-4);
        }
        if(((k>=8)&&(k<12))&&(array_new[k]==1))
        {
            hex[1]=hex[1]+pow(2,k-8);
        }
        if(((k>=12)&&(k<16))&&(array_new[k]==1))
        {
            hex[0]=hex[0]+pow(2,k-12);
        }
    }
    printf("\n");

    char ar[16] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' };
 
    printf("16radix:%c%c%c%c", ar[hex[3]], ar[hex[2]], ar[hex[1]], ar[hex[0]]);

    return 0;
}

你可能感兴趣的:(restful,后端)