fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. void BinFormat(char character, char *text) {
  6. text[8] = '\0';
  7. for (int i = 7; i >= 0; i--) {
  8. int divisor = (int)pow(2, i); //base 2 elevado à posição que está para achar o divisor
  9. printf("[%d, ", divisor);
  10. int cabe = character / divisor; //acha quantas unidades cabem no divisor
  11. printf("%d, ", cabe);
  12. int impar = (cabe % 2 != 0); //o que cabe é impar?
  13. printf("%d] ", impar);
  14. text[i] = impar + '0'; //transfoprma um número em caractere de acordo com a tabela ASCII
  15. }
  16. }
  17.  
  18. int main(void) {
  19. char *text = malloc(9);
  20. BinFormat('A', text);
  21. printf("%s\n", text);
  22. BinFormat('B', text);
  23. printf("%s\n", text);
  24. BinFormat('C', text);
  25. printf("%s\n", text);
  26. }
  27.  
  28. //https://pt.stackoverflow.com/q/286742/101
Success #stdin #stdout 0s 4188KB
stdin
Standard input is empty
stdout
[128, 0, 0] [64, 1, 1] [32, 2, 0] [16, 4, 0] [8, 8, 0] [4, 16, 0] [2, 32, 0] [1, 65, 1] 10000010
[128, 0, 0] [64, 1, 1] [32, 2, 0] [16, 4, 0] [8, 8, 0] [4, 16, 0] [2, 33, 1] [1, 66, 0] 01000010
[128, 0, 0] [64, 1, 1] [32, 2, 0] [16, 4, 0] [8, 8, 0] [4, 16, 0] [2, 33, 1] [1, 67, 1] 11000010