bogdan2004333

Untitled

Nov 20th, 2022
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5.  
  6.  
  7. int DecimalToBinary(int dec,char s[])
  8. {
  9.     int bin[1000] = {};     // array to store binary number
  10.  
  11.     int i = 0;
  12.     while (dec > 0) {       //calculating the binary
  13.         bin[i] = dec % 2;   //storing remainder
  14.         dec = dec / 2;
  15.         ++i;
  16.     }
  17.  
  18.     // printing binary in 8 bit & reverse order
  19.     int bits = 8;
  20.     if (i > 8) {
  21.         bits = 8 * ((i + 7) / 8);
  22.     }
  23.     int n;
  24.     for (int j = bits - 1; j >= 0; j--) {
  25.       n+= sprintf(&s[n],"%d",bin[j]);
  26.     }
  27.  
  28.     return dec;;
  29. }
  30.  
  31. int main() {
  32.  
  33.     int line = 5000000;
  34.     char* byte;
  35.     char *byte1;
  36.     char len[line];
  37.     FILE *text = fopen("text.txt", "rb");
  38.     if (!text) {
  39.         printf("Error of file opening.\n");
  40.         return 1;
  41.     }
  42.     fseek(text, 0, SEEK_END);
  43.     int text_lenght = ftell(text);
  44.     fseek(text, 0, SEEK_SET);
  45.     printf("Количетсво символов в тексте:\n%d\n", text_lenght);
  46.     byte = malloc(sizeof(char*) + 1);
  47.     memset(byte, 0, 8);
  48.     char c = getc(text);
  49.     int i = 0;
  50.     while (!feof(text)) {
  51.         int k = (int) c;
  52.         DecimalToBinary(k,&byte[i]);
  53.         i++;
  54.         c = getc(text);
  55.     }
  56.     for(int k=text_lenght;k>=0;k--){
  57.         printf("%s\n",&byte[k]);
  58.     }
  59.  
  60.     return 0;
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment