Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- int DecimalToBinary(int dec,char s[])
- {
- int bin[1000] = {}; // array to store binary number
- int i = 0;
- while (dec > 0) { //calculating the binary
- bin[i] = dec % 2; //storing remainder
- dec = dec / 2;
- ++i;
- }
- // printing binary in 8 bit & reverse order
- int bits = 8;
- if (i > 8) {
- bits = 8 * ((i + 7) / 8);
- }
- int n;
- for (int j = bits - 1; j >= 0; j--) {
- n+= sprintf(&s[n],"%d",bin[j]);
- }
- return dec;;
- }
- int main() {
- int line = 5000000;
- char* byte;
- char *byte1;
- char len[line];
- FILE *text = fopen("text.txt", "rb");
- if (!text) {
- printf("Error of file opening.\n");
- return 1;
- }
- fseek(text, 0, SEEK_END);
- int text_lenght = ftell(text);
- fseek(text, 0, SEEK_SET);
- printf("Количетсво символов в тексте:\n%d\n", text_lenght);
- byte = malloc(sizeof(char*) + 1);
- memset(byte, 0, 8);
- char c = getc(text);
- int i = 0;
- while (!feof(text)) {
- int k = (int) c;
- DecimalToBinary(k,&byte[i]);
- i++;
- c = getc(text);
- }
- for(int k=text_lenght;k>=0;k--){
- printf("%s\n",&byte[k]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment