Advertisement
ScreenBlack

Char To Bit

May 1st, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.76 KB | None | 0 0
  1. /**
  2. Copyright (c) 2015, ScreenBlack <screenblack at gmail dot com>
  3. All rights reserved.
  4.  
  5. Redistribution and use in source and binary forms, with
  6. or without modification, are permitted provided that the
  7. following conditions are met:
  8.  
  9. 1. Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the following
  11. disclaimer.
  12.  
  13. 2. Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the following
  15. disclaimer in the documentation and/or other materials
  16. provided with the distribution.
  17.  
  18. 3. Neither the name of the copyright holder nor the names of
  19. its contributors may be used to endorse or promote products
  20. derived from this software without specific prior written
  21. permission.
  22.  
  23. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  24. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  25. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  27. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  28. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  30. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  32. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  34. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. POSSIBILITY OF SUCH DAMAGE.
  36. **/
  37.  
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41.  
  42. typedef struct
  43. {
  44.     unsigned char a:1;
  45.     unsigned char b:1;
  46.     unsigned char c:1;
  47.     unsigned char d:1;
  48.     unsigned char e:1;
  49.     unsigned char f:1;
  50.     unsigned char g:1;
  51.     unsigned char h:1;
  52. } Bits;
  53.  
  54. typedef union
  55. {
  56.     Bits list;
  57.     unsigned char A;
  58.     unsigned int Num;
  59. } Char;
  60.  
  61. void carregaCaracter(Char *);
  62. void carregaDecimal(Char *);
  63. void carregaHexadecimal(Char *);
  64. void carregaBinario(Char *);
  65. void mostra(Char);
  66. void menu(void);
  67.  
  68. int main(void)
  69. {
  70.     Char bit;
  71.     int opcao = -1;
  72.  
  73.     while(1)
  74.     {
  75.         menu();
  76.         scanf("%d%*c", &opcao);
  77.  
  78.         switch (opcao)
  79.         {
  80.             case 0:
  81.                 return 0;
  82.  
  83.             case 1:
  84.                 carregaBinario(&bit);
  85.                 mostra(bit);
  86.                 break;
  87.  
  88.             case 2:
  89.                 carregaDecimal(&bit);
  90.                 mostra(bit);
  91.                 break;
  92.  
  93.             case 3:
  94.                 carregaCaracter(&bit);
  95.                 mostra(bit);
  96.                 break;
  97.  
  98.             case 4:
  99.                 carregaHexadecimal(&bit);
  100.                 mostra(bit);
  101.                 break;
  102.  
  103.             default:
  104.                 printf("Opcao invalida.\nEncerrando programa");
  105.         }
  106.     }
  107.  
  108.     return 0;
  109. }
  110.  
  111. void carregaCaracter(Char *bit)
  112. {
  113.     printf("Digite um caracter: ");
  114.     scanf("%c%*c", &bit->A );
  115. }
  116.  
  117. void carregaDecimal(Char *bit)
  118. {
  119.     printf("Digite numero decimal (max 255): ");
  120.     scanf("%u%*c", &bit->Num );
  121. }
  122.  
  123. void carregaHexadecimal(Char *bit)
  124. {
  125.     printf("Digite numero hexadecimal (max FF): ");
  126.     scanf("%x%*c", &bit->Num);
  127. }
  128.  
  129. void carregaBinario(Char *bit)
  130. {
  131.     char binario[9] = {'\0'};
  132.  
  133.     printf("Digite numero binario (8 digitos): ");
  134.     scanf("%s%*c", binario );
  135.  
  136.     if ( binario[0] == '\0' )
  137.         bit->list.h = 0;
  138.     else
  139.         bit->list.h = binario[0]-48;
  140.  
  141.     if ( binario[1] == '\0' )
  142.         bit->list.g = 0;
  143.     else
  144.         bit->list.g = binario[1]-48;
  145.  
  146.     if ( binario[2] == '\0' )
  147.         bit->list.f = 0;
  148.     else
  149.         bit->list.f = binario[2]-48;
  150.  
  151.     if ( binario[3] == '\0' )
  152.         bit->list.e = 0;
  153.     else
  154.         bit->list.e = binario[3]-48;
  155.  
  156.     if ( binario[4] == '\0' )
  157.         bit->list.d = 0;
  158.     else
  159.         bit->list.d = binario[4]-48;
  160.  
  161.     if ( binario[5] == '\0' )
  162.         bit->list.c = 0;
  163.     else
  164.         bit->list.c = binario[5]-48;
  165.  
  166.     if ( binario[6] == '\0' )
  167.         bit->list.b = 0;
  168.     else
  169.         bit->list.b = binario[6]-48;
  170.  
  171.     if ( binario[7] == '\0' )
  172.         bit->list.a = 0;
  173.     else
  174.         bit->list.a = binario[7]-48;
  175. }
  176.  
  177. void mostra(Char bit)
  178. {
  179.         printf("\n###########\nChar: %c\nDec: %d\nOctal: %o\nHexa: %X\n", bit.A, bit.A, bit.A, bit.A);
  180.         printf("Binario: %d%d%d%d%d%d%d%d\n###########\n\n", bit.list.h, bit.list.g, bit.list.f, bit.list.e, bit.list.d, bit.list.c, bit.list.b, bit.list.a);
  181. }
  182.  
  183. void menu(void)
  184. {
  185.     printf("---- Conversor NERD ----\n\n");
  186.     printf("1 - Leitura binaria\n");
  187.     printf("2 - Leitura decimal\n");
  188.     printf("3 - Leitura caracter\n");
  189.     printf("4 - Leitura hexadecimal\n\n");
  190.     printf("0 - Encerra programa\n\n");
  191.     printf("Opcao: ");
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement