Created
July 12, 2009 00:36
-
-
Save nhocki/dc9045b14f52ddf3a815 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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