74 lines
2.1 KiB
C
74 lines
2.1 KiB
C
#include "stdio.h"
|
|
|
|
// ladicí výpisy
|
|
#ifdef NDEBUG
|
|
#define debug(...)
|
|
#else
|
|
#define debug(...) do{fprintf(stderr, __VA_ARGS__);}while(0)
|
|
#endif
|
|
|
|
int main() {
|
|
|
|
int i = 0;
|
|
unsigned char buf[8];
|
|
char in = '\0';
|
|
char in_tmp = 0;
|
|
unsigned char coefs[8] = {128, 64, 32, 16, 8, 4, 2, 1};
|
|
|
|
|
|
// hláška k zadání vstupu
|
|
printf("Zadejte textovy vstup (rucni ukonceni pomoci 2xCTRL+Z na Windows (2xCTRL+D na Linuxu)): ");
|
|
|
|
// čtení prvního znaku vstupu
|
|
in = fgetc(stdin);
|
|
|
|
// odřádkování po zadání dat, protože už nemáme Enter
|
|
fputc('\n', stdout);
|
|
|
|
// hláška o výstupu
|
|
printf("Zakodovany binarni vystup: ");
|
|
|
|
// cyklus zpracování vstupu
|
|
// zrušena možnost Enter na konci, nešly načítat víceřádkové soubory
|
|
// technický popis chování:
|
|
//Using Control-D, you cause the terminal driver to make all the input typed so far on the line available
|
|
//for reading, even if you've not typed return. If you type, say, abc and then Control-D,
|
|
//the terminal driver makes the three characters abc available (and Control-D is not seen by the program).
|
|
//If you type Control-D again, or type return and then Control-D, then there are no more characters available,
|
|
//so the underlying read() system call returns 0, which the standard I/O system interprets as EOF.
|
|
while(in != EOF /*&& in != '\n'*/) {
|
|
|
|
// ladicí výpis aktuálního znaku
|
|
debug("%c\n", in);
|
|
|
|
// kontrola vstupu
|
|
if(in < 0 && in != 255) {
|
|
fprintf(stderr, "CHYBA: Neplatny znak na %i. pozici, lze zadat pouze ASCII znaky.\n", i+1);
|
|
return -1;
|
|
}
|
|
|
|
// binární hodnota z desítkové reprezentace ASCII znaku
|
|
in_tmp = in;
|
|
for(int j=0; j<8; j++) {
|
|
buf[j] = in_tmp / coefs[j];
|
|
debug("DEBUG%i: -%c- -%i- [%i%i%i%i%i%i%i%i]\n", j+1, in, in_tmp, buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);
|
|
in_tmp = in_tmp - (buf[j] * coefs[j]);
|
|
}
|
|
|
|
// výpis osmice binárních znaků
|
|
for(int j=0; j<8; j++) {
|
|
fputc(buf[j]+'0', stdout);
|
|
}
|
|
|
|
|
|
// čtení dalšího ASCII znaku
|
|
in = fgetc(stdin);
|
|
i++;
|
|
}
|
|
|
|
// odřádkování na konci
|
|
fputc('\n', stdout);
|
|
|
|
return 0;
|
|
}
|