Skip to content

Instantly share code, notes, and snippets.

@airbugg
Created November 10, 2016 14:46
Show Gist options
  • Save airbugg/20704a7c3320cc1b3bc3817c0f7660ce to your computer and use it in GitHub Desktop.
Save airbugg/20704a7c3320cc1b3bc3817c0f7660ce to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int *char2ascii(int arr[8], char c);
void convertToBinary(int arr[8], char c);
void printBinart(int arr[8]);
int main(int argc, char **argv) {
char c;
int arr[8];
int flag = argc > 1 ? strcmp(argv[1], "-l") : 1;
while ((c = getchar() ) != EOF) {
if (flag) {
if (c == 10)
printf("\n");
else {
convertToBinary(arr, c);
printBinart(arr);
}
}
}
return 0;
}
void convertToBinary(int arr[8], char c) {
int i;
for (i = 0; i < 8; ++i) {
if (c % 2 == 0)
arr[i] = 0;
else
arr[i] = 1;
c = c / 2;
}
}
void printBinart(int arr[8]) {
int j;
for (j = 0; j < 8; ++j) {
printf("%d", arr[j]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment