Guest User

Untitled

a guest
Jul 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.87 KB | None | 0 0
  1. unsigned char Xbee::readloop()
  2.     unsigned char binary;
  3.     string binstr;
  4.     printf("Enter a binary number from 00000000 to 00011111 \n");
  5.     cin >> binstr;
  6.     while (StrErrorCheck(binstr) == true) {
  7.         printf("Error: Please input again\n");
  8.         cin >> binstr;
  9.     }
  10.     binary = bstr2dec(binstr);
  11.     return binary;
  12. }
  13.  
  14.  
  15. unsigned char Xbee::bstr2dec(string str)
  16. {
  17.     unsigned char result = 0;
  18.     unsigned int i;
  19.  
  20.     for (i = 0; i < str.length(); i++) {
  21.         result = 2 * result + (str[i] - '0');
  22.     }
  23.     return result;
  24. }
  25.  
  26. bool Xbee::StrErrorCheck(string str) {
  27.     bool error = false;
  28.     unsigned int i;
  29.     if (str.length() > 8 || str.length() == 0) {
  30.         error = true;
  31.     }
  32.     for (i = 0; i < str.length(); i++) {
  33.         if( (str[i] != '1') && (str[i] != '0') ) {
  34.             error = true;
  35.         }
  36.         if( (i == 5 || i == 6 || i == 8) && (str[i] == 1) ) {
  37.             error = true;
  38.         }
  39.     }
  40.     return error;
  41. }
Add Comment
Please, Sign In to add comment