We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › number to base string conversion
Page Index Toggle Pages: 1
number to base string conversion (Read 552 times)
number to base string conversion
Dec 23rd, 2005, 4:36pm
 
hey all, I hope everyone is having a great holiday! I want to create a function which converts 10 based numbers to other ones. I am getting an error related with my return inside the function. here is the code I have:

int output;// spits the number for recursive arg.
void setup() {
numberToBaseString(24,5);
}

void draw() {

}

numberToBaseString(int num, int base) {
 if(num < base ) {
    output = num;
   }
   else
   {
   int rem = num%base;
    output = rem;
   int reducedNum = (num - rem) / base;
   int rest = numberToBaseString(reducedNum, base);
   output = rem + output;
// this is wrong I guess because I am adding rem + output
// as integers. I need to add them as strings. Any guides?
   }
 return output;
// println(output);
   
}










here is the error I have.


/tmp/build/Temporary_8858_6605.java:10:2:10:19: Semantic Error: The name of the constructor "numberToBaseString" does not match the name of the class "Temporary_8858_6605". Assuming it is a method with missing return type.

I really welcome any corrections from you math guys. Thanks in advance
ilteris.
Re: number to base string conversion
Reply #1 - Dec 23rd, 2005, 6:43pm
 
Hi ilteris,

take a look into this one, it should work:

void setup() {  
 println(numberToBaseString(24, 5));
}

void draw() {
}

String numberToBaseString(int num, int base) {
 String output = "";

 if(num < base ) {  
   output = String.valueOf(num);
 }
 else
 {
   int rem = num % base;
   num = num / base;

   output = numberToBaseString(num, base) + String.valueOf(rem);
 }
 return output;
}  
Re: number to base string conversion
Reply #2 - Dec 23rd, 2005, 9:14pm
 
Library class Integer has static method toString(int i, int radix) that should do what you want.
Re: number to base string conversion
Reply #3 - Dec 23rd, 2005, 10:08pm
 
thanks guys! you are a saver FloHimself! works smoothly.
Page Index Toggle Pages: 1