Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* baseX-decimal-hw4.c
- Converts a base 64 number into decimal
- Created by Xiuwen Liu for Homework #4, Problem #1
- */
- #include <ctype.h>
- #include <stdio.h>
- #include <string.h>
- /* Based on the following base 64 encoding:
- '0' to '9' map to 0 to 9
- 'A' to 'Z' map to 10 to 35
- 'a' to 'z' map to 36 to 61
- '>' maps to 62
- '?' maps tp 63
- */
- int convert2dec64(char *str)
- {
- int j, val, base, chval;
- base = 64;
- val = 0;
- j = 0;
- while (str[j] > 13) {
- if ( (str[j] > 47) && (str[j] < 58)) {
- chval = str[j] - 48;
- }
- else if ( (str[j] > 64) && (str[j] < 91) ) {
- chval = str[j]-55;
- }
- else if ( (str[j] > 96) && (str[j] < 123)) {
- chval = str[j] - 61;
- }
- else if ( (str[j] == 62) || (str[j] ==63) ) {
- chval = str[j];
- } else {
- val = -1;
- break;
- }
- val = val * base + chval;
- j++;
- }
- return val;
- }
- int main(int argc, char *argv[])
- {
- int X, res;
- char str[256];
- X = 64;
- printf("Please a number base %d: ", X);
- scanf("%s", str);
- res = convert2dec64(str);
- if (res != -1) {
- printf("The decimal value is %d\n", res);
- }
- else {
- printf("The input is not a valid base64 number.\n");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement