Advertisement
roronoa

ébauche conversion binaire vers hexa

Aug 5th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.20 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int bitToNombre(int n, int tab[n])
  6. {
  7.     int retour = 0;
  8.     int j = 0;
  9.     for(int i = n-1 ; i >= 0 ; i--)
  10.     {
  11.         if(tab[i] == 1)
  12.         {
  13.             retour = (int) retour + pow(2,j);
  14.         }
  15.         j++;
  16.     }
  17.     return retour;
  18. }
  19. void reverse(int n, int tab[n])
  20. {
  21.     int copie[n];
  22.     int j = 0;
  23.     for(int i = n-1; i >=0; i--)
  24.     {
  25.         copie[i] = tab[j];
  26.         j++;
  27.     }
  28.     for(int i = 0; i < n; i++)
  29.     {
  30.         tab[i] = copie[i];
  31.     }
  32. }
  33. char affichHexa(int n)
  34. {
  35.     char retour;
  36.     if(n >= 10)
  37.         //printf("%c",'a' + n%10);
  38.         retour = 'a' +  (char) n%10;
  39.     else
  40.         //printf("%d",n);
  41.         retour = (char) n + '0';
  42.     return retour;
  43. }
  44. void remplir(int n, int tab[n], int i)
  45. {
  46.     for(int k = i; k < n; k++)
  47.         tab[k] = 0;
  48. }
  49. void afficherTab(int n, int tab[n])
  50. {
  51.     printf("\n");
  52.     for(int i = 0; i < n; i++)
  53.     {
  54.         printf("%d ",tab[i]);
  55.     }
  56. }
  57. int main()
  58. {
  59.     char T[16];
  60.     int tabB[4];
  61.     for(int z = 0; z < 4; z++)
  62.         tabB[z] = 0;
  63.     fgets(T, 16, stdin);
  64.     int fin = strlen(T)-1;
  65.     int j = 0;
  66.     printf("0x");
  67.     int nonFini = 0;
  68.     int compteur = 0;
  69.     char reponse[16];
  70.     reponse[0] = '\0';
  71.     for(int i = fin; i >= 2; i--)
  72.     {
  73.         int chiffre = T[i] - '0';
  74.         //printf("chiffre : %d\n",chiffre);
  75.         if(j <= 2)
  76.         {
  77.             tabB[j] = chiffre;
  78.             j++;
  79.             if(i == 2)
  80.                 nonFini = 1;
  81.         }
  82.         else if(j == 3)
  83.         {
  84.             tabB[j] = chiffre;
  85.             j = 0;
  86.             reverse(4,tabB);
  87.             int reponse = bitToNombre(4,tabB);
  88.             //reponse[compteur] =
  89.             printf("%c",affichHexa(reponse));
  90.             for(int z = 0; z < 4; z++)
  91.                 tabB[z] = 0;
  92.  
  93.             compteur++;
  94.         }
  95.  
  96.     }
  97.     if(nonFini == 1)
  98.     {
  99.         //afficherTab(4,tabB);
  100.         reverse(4,tabB);
  101.         int reponse = bitToNombre(4,tabB);
  102.         //printf("\nreponse : %d ",reponse);
  103.         //printf("ici\n");
  104.         affichHexa(reponse);
  105.     }
  106.     //printf("compteur :%d\n",compteur);
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement