Skip to content

Instantly share code, notes, and snippets.

@nhocki
Created July 12, 2009 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhocki/dc9045b14f52ddf3a815 to your computer and use it in GitHub Desktop.
Save nhocki/dc9045b14f52ddf3a815 to your computer and use it in GitHub Desktop.
//Convierte un numero N en base B a decimal
public int toDecimal(int n, int b)
{
int result=0;
int multiplier=1;
while(n>0)
{
result+=n%10*multiplier;
multiplier*=b;
n/=10;
}
return result;
}
//En Java se puede utilizar: return Integer.parseInt(""+n,b);
//Retorna un numero N (decimal) en una base B
public int fromDecimal(int n, int b)
{
int result=0;
int multiplier=1;
while(n>0)
{
result+=n%b*multiplier;
multiplier*=10;
n/=b;
}
return result;
}
//Para bases >= 10
public String fromDecimal2(int n, int b)
{
String chars="0123456789ABCDEFGHIJ";
String result="";
while(n>0)
{
result=chars.charAt(n%b) + result;
n/=b;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment