Binary 将任何基转换为二进制

Binary 将任何基转换为二进制,binary,base,Binary,Base,是否有一种算法或公式可以将任意基数n(比如2到36)转换为二进制?我在网上到处找了找,找不到我想要的东西。有什么东西可以帮你开始 unsigned strtou(const char *s, unsigned base) { unsigned y = 0; while (*s) { unsigned digit; if (isdigit(*s)) digit = ch - '0'; else if (isupper(*s)) digit = ch - 'A'

是否有一种算法或公式可以将任意基数n(比如2到36)转换为二进制?我在网上到处找了找,找不到我想要的东西。

有什么东西可以帮你开始

unsigned strtou(const char *s, unsigned base) {
  unsigned y = 0;
  while (*s) {
     unsigned digit;
     if (isdigit(*s)) digit = ch - '0';
     else if (isupper(*s)) digit = ch - 'A' + 10;
     else if (islower(*s)) digit = ch - 'a' + 10;
     else Handle_IllegalDigit();
     if (digit >= base) Handle_IllegalDigit();

     y = y*base + digit;
     s++;
  }
  return y;
}