Skip to content

Instantly share code, notes, and snippets.

@mustooch
Created May 29, 2020 20:05
Show Gist options
  • Save mustooch/fb22f2e5b6d0b0c8357515d15f88ce5c to your computer and use it in GitHub Desktop.
Save mustooch/fb22f2e5b6d0b0c8357515d15f88ce5c to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
#define ERR -1
int hua2dec(char *str) {
long int res = 0;
int len = strlen(str);
if (str[0] != 'h' || str[len-1] != 'a') return ERR;
int power = len-3;
for (int i=1; i<=strlen(str)-2; i++) {
if (str[i] == 'U') {
res += ceil((int)pow(2, power));
power--;
} else if (str[i] == 'u') {
power--;
} else {
return ERR;
}
}
return res;
}
int main () {
printf("hUUuuuUua : %u\n", hua2dec("hUUuuuUua"));
printf("huuUUuua : %u\n", hua2dec("huuUUuua"));
printf("huuuuUa : %u\n", hua2dec("huuuuUa"));
printf("hUUUUUa : %u\n", hua2dec("hUUUUUa"));
return 0;
}
@mustooch
Copy link
Author

numbers take the form hUuUuUua where 'h' and 'a' get removed, and the U's and u's get processed as 1's and 0's (respectively) in binary - returns in decimal form.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment