Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #include <string.h>
- #include <stdlib.h>
- //#include <conio.h>
- int input[8], code[12];
- void chtoi(int* arr, char* str, int len)
- {
- int i;
- for(i = 0; i < len; i++)
- {
- if(str[i] == 48)
- arr[i] = 0;
- if(str[i] == 49)
- arr[i] = 1;
- }
- }
- void hamming(int* arr)
- {
- int P1, P2, P3, P4;
- int i, j = 0;
- P1 = arr[0] ^ arr[1] ^ arr[3] ^ arr[4] ^ arr[6];
- P2 = arr[0] ^ arr[2] ^ arr[3] ^ arr[5] ^ arr[6];
- P3 = arr[1] ^ arr[2] ^ arr[3] ^ arr[7];
- P4 = arr[4] ^ arr[5] ^ arr[6] ^ arr[7];
- code[1-1] = P1;
- code[2-1] = P2;
- code[4-1] = P3;
- code[8-1] = P4;
- for(i = 0; i < 12; i++)
- {
- if(i != 0 && i != 1 && i != 3 && i != 7)
- {
- code[i] = arr[j];
- j++;
- }
- }
- }
- int main()
- {
- int len, i;
- char chinput[8];
- printf("\nEnter the 8-bit binary number whose Hamming code you wish to find -: ");
- gets(chinput);
- if(strlen(chinput) != 8)
- {
- printf("\nThe number is not valid.");
- printf("\nHamming code generation failed.\n");
- //getch();
- exit(1);
- }
- chtoi(input, chinput, 8);
- hamming(input);
- printf("\nThe Hamming code is displayed as follows -: ");
- for(i = 0; i < 12; i++)
- printf("%d", code[i]);
- printf("\n");
- //getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement