vimja

Untitled

Jul 10th, 2012
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. // Copyright (c) 2012 by Bash Vi
  2. // This code is released under the terms
  3. // of the WTFPL (What the Fuck Pulic License) version 2.0
  4. // http://sam.zoy.org/wtfpl/
  5.  
  6. // Converts a string of up to 8
  7. // 1es and 0es to the matching (ASCII) character
  8. #include <bitset>
  9. #include <string.h>
  10. #include <iostream>
  11. #include <cstring>
  12.  
  13. using namespace std;
  14.  
  15. int main(int ac, char* av[])
  16. {
  17.   if(strlen(av[1]) < 9)
  18.   {
  19.     unsigned char a;
  20.     // put input into a string
  21.     string str(av[1]);
  22.     try{
  23.       bitset<8> bs(str);
  24.       //cout << bs.to_string() << endl; // debug statement
  25.       memcpy(&a, &bs, 1);
  26.       cout << a << endl;
  27.     }
  28.     catch (exception e)
  29.     {
  30.       cout << "You may only enter 1s and 0z!" << endl;
  31.     }
  32.   }
  33.   else
  34.   {
  35.     cerr << "array too long" << endl;
  36.   }
  37.  
  38.   return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment