Advertisement
SVXX

12-bit Hamming Code

Jan 31st, 2014
415
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. //#include <conio.h>
  6.  
  7. int input[8], code[12];
  8.  
  9. void chtoi(int* arr, char* str, int len)
  10. {
  11.     int i;
  12.     for(i = 0; i < len; i++)
  13.     {
  14.         if(str[i] == 48)
  15.             arr[i] = 0;
  16.         if(str[i] == 49)
  17.             arr[i] = 1;
  18.     }
  19. }
  20.  
  21. void hamming(int* arr)
  22. {
  23.     int P1, P2, P3, P4;
  24.     int i, j = 0;
  25.     P1 = arr[0] ^ arr[1] ^ arr[3] ^ arr[4] ^ arr[6];
  26.     P2 = arr[0] ^ arr[2] ^ arr[3] ^ arr[5] ^ arr[6];
  27.     P3 = arr[1] ^ arr[2] ^ arr[3] ^ arr[7];
  28.     P4 = arr[4] ^ arr[5] ^ arr[6] ^ arr[7];
  29.     code[1-1] = P1;
  30.     code[2-1] = P2;
  31.     code[4-1] = P3;
  32.     code[8-1] = P4;
  33.  
  34.     for(i = 0; i < 12; i++)
  35.     {
  36.         if(i != 0 && i != 1 && i != 3 && i != 7)
  37.         {
  38.             code[i] = arr[j];
  39.             j++;
  40.         }
  41.     }
  42. }
  43.  
  44. int main()
  45. {
  46.     int len, i;
  47.     char chinput[8];
  48.     printf("\nEnter the 8-bit binary number whose Hamming code you wish to find -: ");
  49.     gets(chinput);
  50.    
  51.     if(strlen(chinput) != 8)
  52.     {
  53.         printf("\nThe number is not valid.");
  54.         printf("\nHamming code generation failed.\n");
  55.         //getch();
  56.         exit(1);
  57.     }
  58.     chtoi(input, chinput, 8);
  59.     hamming(input);
  60.     printf("\nThe Hamming code is displayed as follows -: ");
  61.     for(i = 0; i < 12; i++)
  62.         printf("%d", code[i]);
  63.     printf("\n");  
  64.     //getch();
  65.     return 0;
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement