Advertisement
amigojapan

Binary to Ascii and Ascii to Binary converter in C

Mar 14th, 2015
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. //This program is by Usmar A Padow (amigojapan) usmpadow@gmail.com
  2. //converts ascii to binary and binary to ascii in C
  3. //source https://www.youtube.com/watch?v=XykJzVbrf3s
  4. #define MASK0 0x01
  5. #define MASK1 0x02
  6. #define MASK2 0x04
  7. #define MASK3 0x08
  8. #define MASK4 0x10
  9. #define MASK5 0x20
  10. #define MASK6 0x40
  11. #define MASK7 0x80
  12.  
  13. unsigned char AtoB(char *ASCII) {//returns a unsigned char that is the binary value of the ascii sent
  14.     //GeDaMo: amigojapan: well, those masks are a bit pointless, MASK0 is 1 << 0, MASK1 is 1 << 1 etc.
  15.     //GeDaMo: amigojapan: also, |= exists so you can write a |= b instead of a = a | b
  16.     unsigned char result;//this will contain the binary result
  17.     if(ASCII[7]=='1') result = result | MASK0;
  18.     if(ASCII[6]=='1') result = result | MASK1;
  19.     if(ASCII[5]=='1') result = result | MASK2;
  20.     if(ASCII[4]=='1') result = result | MASK3;
  21.     if(ASCII[3]=='1') result = result | MASK4;
  22.     if(ASCII[2]=='1') result = result | MASK5;
  23.     if(ASCII[1]=='1') result = result | MASK6;
  24.     if(ASCII[0]=='1') result = result | MASK7;
  25.     return result;
  26. }
  27. #include<string.h>
  28. #include<stdio.h>
  29.  
  30. unsigned char AtoB2(char *ASCII) {//returns a unsigned char that is the binary value of the ascii sent
  31.     //GeDaMo: amigojapan: well, those masks are a bit pointless, MASK0 is 1 << 0, MASK1 is 1 << 1 etc.
  32.     //GeDaMo: amigojapan: also, |= exists so you can write a |= b instead of a = a | b
  33.     unsigned char result;//this will contain the binary result
  34.     result=0;
  35.     //printf("strlen(ASCII):%lu, ",strlen(ASCII));
  36.     //if(strlen(ASCII)>sizeof(result)) return result;//exit gracefully if the ASCII is too long for the architecture we are on
  37.     int up, down;
  38.     for(up=strlen(ASCII)-1,down=0;down < strlen(ASCII);up--,down++) {
  39.         int mask;
  40.         mask=1 << down;
  41.         if(ASCII[up]=='1') result |= mask;
  42.     }
  43.     return result;
  44. }
  45.  
  46. //binary conversion function Binary to Ascii by GeDaMo
  47. void BtoA(unsigned char ch, char *b) {
  48.     int mask = 0x80;
  49.     int i;
  50.    
  51.     for (i = 0; i < 8; ++i) {
  52.         b[i] = ch & mask ? '1' : '0';
  53.         mask >>= 1;
  54.     }
  55.    
  56.     b[i] = '\0';
  57. }
  58.  
  59. int main(void) {//test program to test both functions
  60.     //char ASCII[9]="10101010";
  61.     char ASCII[9]="1001";
  62.     char ASCII2[9];
  63.     unsigned char BINARY;
  64.     BINARY = AtoB2(ASCII);
  65.     BtoA(BINARY,ASCII2);
  66.     printf("%s",ASCII2);
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement