Skip to content

Instantly share code, notes, and snippets.

@takano32
Created May 16, 2009 13:03
Show Gist options
  • Save takano32/112672 to your computer and use it in GitHub Desktop.
Save takano32/112672 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int b(char* str) {
int r = 0;
char* p = str;
for(r = 0; *p; p++) {
r <<= 1;
r += *p - '0';
}
return r;
}
int o(char* str) {
int r = 0;
char* p = str;
for(r = 0; *p; p++) {
r <<= 4;
if('0' < *p && *p < '9') {
r += *p - '0';
}
if('a' < *p && *p <'z') {
r += *p - 'a' + 10;
}
if('A' < *p && *p < 'Z') {
r += *p - 'A' + 10;
}
}
return r;
}
int
main(int argc, char* argv[]) {
int hoge = b("11111111");
printf("%08X\n", hoge);
int fuga = o("ffffffff");
printf("%08X\n", fuga);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment