java - Converting a Base Ten Number to Any Number -
there several similar threads around here pose question mine unable find solution works relatively simply. wondering if guys had ideas.
i have code take base ten number , convert number in base 1 11. it's when start getting things hexadecimal format struggle because need letters in addition numbers base. i'm sure solution pretty simple escaping me. appreciated.
import static java.lang.system.*; public class tentoany { private int base10; private int newbase; public tentoany(int ten, int base) { base10 = ten; newbase = base; } public void setnums(int ten, int base) { base10 = ten; newbase = base; } public string getnewnum() { string newnum=""; int original = base10; while(original > 0) { newnum = original%newbase + newnum; original = original/newbase; } return newnum; } public string tostring() { string complete = base10 + " base 10 " + getnewnum() + " in base " + newbase; return complete; } }
there number of possible solutions. i'm including instructional value, not because think it's better other answer. replace:
newnum = original%newbase + newnum;
with
int digitvalue = original % newbase; char digit = (digitvalue < 10) ? ('0' + digitvalue) : ('a' + (digitvalue - 10)); newnum = digit + newnum;
Comments
Post a Comment