Advertisement
AntonioVillanueva

Test conversion 16 caractères binaires

Jun 13th, 2022
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.96 KB | None | 0 0
  1.  
  2.  * Conversion d'une "chaîne" binaire composée de 0's et 1's
  3. * sur 16 caractères en deux "octets" bytes
  4. *
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. void printBin (unsigned char byte);
  9.  
  10. int main(){
  11.     char tmp[] ="0101010101010101"; //0101 0101 = 0x55   0101 0101 = 0x55
  12.     unsigned char byte;//Variable auxiliaire
  13.  
  14.     for (int i=0;i<strlen (tmp);i++){ //parcourt la chaîne de 16 bits-chars
  15.        
  16.         byte += tmp[i] =='1' ? 1:0;
  17.        
  18.         if ( (i==7) || (i==15)){
  19.             printf("\nHEX 0x%02X \n",byte);//Affiche l'octet sous forme HEX
  20.             printBin( byte );//Affiche l'octet sous forme BIN
  21.            
  22.             byte=0;//Reset variable auxiliare
  23.         }else {byte = byte<<1;}//décale un "bit" vers la gauche <<1
  24.     }
  25.    
  26.     return 0;
  27. }
  28.  
  29. //Fonction pour imprimer , "tester" le  byte binaire
  30. void printBin (unsigned char byte){
  31.     printf ("HEX in printBin  0x%02X\n",byte);
  32.     for (int i=0;i<8;i++){
  33.         printf ("%c", byte & 0x80 ? '1': '0');
  34.         byte=byte<<1;
  35.     }
  36.     printf ("%c",'\n');
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement