Skip to content

Instantly share code, notes, and snippets.

@werdl
Last active April 6, 2024 08:13
Show Gist options
  • Save werdl/672b7271a11a92ba7ff45a5b325b6ef7 to your computer and use it in GitHub Desktop.
Save werdl/672b7271a11a92ba7ff45a5b325b6ef7 to your computer and use it in GitHub Desktop.
Base converter, golfed for source size and binary size (only works with tcc)
#define z int
char*d="0123456789abcdefghijklmnopqrstuvwxyz";b(z a,char*n){if(a==16&n[0]=='0'&n[1]=='x'){n+=2;}if(a==2&n[0]=='0'&n[1]=='b'){n+=2;}z r=0;for(z i=0;n[i]!='\0';i++){z c=n[i];z g=0;for(z j=0;d[j]!='\0';j++){if(d[j]==c){g=j;break;}}r=r*a+g;}return r;}char*v(z s,z u){z p=1;z m=u;while(m>=s){m/=s;p*=s;}char e[100];z i;while(p>0){z t=u/p;e[i]=d[t];i++;u-=t*p;p/=s;}e[i]='\0';return e;}main(z c,char**r){z a=b(10,r[2]);char*n=r[1];if(c>3){printf("%s\n",v(a,b(10,n)));}else{printf("%d\n",b(a, n));}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment