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 › BsaeString to Value
Page Index Toggle Pages: 1
BsaeString to Value (Read 653 times)
BsaeString to Value
Dec 31st, 2005, 10:37pm
 
Hello List,

So  after I got my answer about number To Base conversion, some unfortunate incidents came across, I was taken to a hospital and stayed there  for 6 days after a surgery which shows I have an peptic ulcer. But that's another subject.

My question is now I am trying to create this function where I can convert Base numbers to actual decimal values. Here what I got so far, I am having a hard time using StringBuffer which is throwing me an error, also I truly believe I am making things unnecessarily more complicated - yeah it is a gift :) -

here is thecode:

Code:

StringBuffer digitString = new StringBuffer("");
// to keep my base String which is going to be modifiable.
void setup() {
 println(baseStringToValue(243,10));
}

void draw() {
}

int baseStringToValue(StringBuffer digitString, int base) {
 int output;

 if ( digitString = "") {
// halt thefunction if there  is no value
   output = 0;
 }
 else
 {
   char last = digitString.charAt(digitString.length()-1);
// getting the last element of the BaseString (3) in this sense
    output = String.valueOf(last);
digitString.deleteCharAt(digitString.length()-1);
// cleaning it from the StringBuffer to be able to calculatetherest in recursion
   String rem = digitString.toString();
//making my StringBufferstring again.
   int remVal = baseStringToValue(digitString, base);
   output += base * remVal;
// adding values to output
 }
 return output;
}




I would be grateful for any commentsand modifications on thecode.
Happy new year everyone.

ilteris.
Re: BsaeString to Value
Reply #1 - Jan 1st, 2006, 12:56pm
 
Will Integer method parseInt(String s, int radix) do what you want?
Re: BsaeString to Value
Reply #2 - Jan 1st, 2006, 4:26pm
 
thanks for the feedback. I am not sure if that's what I need. I am just trying to create this from scratch here. I happened to dig more and approached it from a differentpoint of view but seems I stumbled accross new problems.

Code:

void setup() {
println (baseStringToValue(245,10)); // first arg should be string.
}

void draw() {
}

int baseStringToValue(String digitString, int base) {
String output;

if(digitString == "") {
output = "";
}
else
{
output = digitString.substring(digitString.length()-1,digitString.length());
String remString = digitString.substring(0,digitString.length()-1);
int remStringVal = baseStringtoValue(remString, base);
output = output + (base * remStringVal);
}
return output;
}




any help would be appreciated.
ilteris.
Re: BsaeString to Value
Reply #3 - Jan 2nd, 2006, 2:13pm
 
I would encourage you to continue if this is an exercise in learning Java, recursion, base conversion, etc.; but discourage this in production code.

Here are a few comments and suggestions regarding the current method:
- The method is declared to return an int, but returns a String. This can be corrected by declaring the type of variable "output" as int, and initializing "output" to 0. If "digitString" is not the empty String (""), first set "output" to the int for the last char of "digitString" (some char computation is needed here), and then continue with the recursion.
- The char computation is a bit tricky. For example, if the base is 16, you have to deal with hex digits 'a', 'A', etc. You may make faster development progress if you initially require that "base" is <= 10.
- The recursive call should be to "baseStringToValue", not "baseStringtoValue".
- The correct way to test for the empty String is to use the  "equals" method, as in
 if(digitString.equals(""))

Here's how you delegate the computation to Integer method parseInt(String s, int radix):

int baseStringToValue(String digitString, int base) {
 return Integer.parseInt(digitString, base);
}
Re: BsaeString to Value
Reply #4 - Jan 6th, 2006, 11:15am
 
thanks for the heads up rick. yeah those are just exercises. Gonna bug you with my questions in the future Cheesy

Page Index Toggle Pages: 1