SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* baseX-decimal-hw4.c
  2.    Converts a base 64 number into decimal
  3.    Created by Xiuwen Liu for Homework #4, Problem #1
  4. */
  5. #include <ctype.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. /* Based on the following base 64 encoding:
  9. '0' to '9' map to 0 to 9
  10. 'A' to 'Z' map to 10 to 35
  11. 'a' to 'z' map to 36 to 61
  12. '>' maps to 62
  13. '?' maps tp 63
  14. */
  15.  
  16. int convert2dec64(char *str)
  17. {
  18.     int j, val, base, chval;
  19.     base = 64;
  20.     val = 0;
  21.     j = 0;
  22.     while (str[j] > 13) {
  23.         if ( (str[j] > 47) && (str[j] < 58)) {
  24.             chval =  str[j] - 48;
  25.         }
  26.         else if ( (str[j] > 64) && (str[j] < 91) ) {
  27.             chval =  str[j]-55;
  28.         }
  29.         else if ( (str[j] > 96) && (str[j] < 123)) {
  30.             chval = str[j] - 61;
  31.         }
  32.         else if ( (str[j] == 62) || (str[j] ==63) ) {
  33.             chval =  str[j];
  34.         } else {
  35.             val = -1;
  36.             break;
  37.         }
  38.         val = val * base + chval;
  39.         j++;
  40.     }
  41.     return val;
  42. }
  43.  
  44. int main(int argc, char *argv[])
  45. {
  46.     int X, res;
  47.     char str[256];
  48.     X = 64;
  49.     printf("Please a number base %d: ", X);
  50.     scanf("%s", str);
  51.     res = convert2dec64(str);
  52.     if (res != -1) {
  53.         printf("The decimal value is %d\n", res);
  54.     }
  55.     else {
  56.         printf("The input is not a valid base64 number.\n");
  57.     }
  58.     return 0;
  59. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top