Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #3217

Re: Need help to convert

From "tar" <tar@THRWHITE.remove-dii-this>
Subject Re: Need help to convert
Message-ID <ymitzjv33wi.fsf@blackcat.isi.edu> (permalink)
Newsgroups comp.lang.java.gui
References <14-dnfLeruzT4V3anZ2dnUVZ_tKinZ2d@comcast.com>
Date 2011-04-27 15:43 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
Lew <lew@lewscanon.com> writes:

> Katak wrote:
> > public class BinaryToDecimal {
> > 	public static void main(String[] args) {
> > 		   String bin;
> > 		       result += (( bin[i] - 48)*(cubed));
...
> It seems that you are looking for String#charAt()
> <http://java.sun.com/javase/6/docs/api/java/lang/String.html#charAt(int)>
> but be aware that character encoding can differ, and '48' might not be
> the magic number you're looking for.  Also, what if the /i/th character
> is not a digit?

And to help out with those problems, it would be wise to look at the
Character class, which has some nice useful methods like "isDigit" and
"digit" which will handle all of those particular issues for you.

As a general rule, there are a gazillion classes that come standard with
Java compilers, and you can save a lot of time and aggravation by using
them.  Porting your C++ algorithm can be a useful initial exercise for
gaining some familiarity with the language, but you will eventually want
to learn to write Java code, rather than C++ code in Java.

So, looking at some of the other built-in classes, you might discover
that the entire problem of BinaryToDecimal conversion is already handled
for you by the language itself:

  Integer.parseInt(inputString, 2).toString();



-- 
Thomas A. Russ,  USC/Information Sciences Institute

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Re: Need help to convert "Lew" <lew@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000
  Re: Need help to convert "tar" <tar@THRWHITE.remove-dii-this> - 2011-04-27 15:43 +0000

csiph-web