[2461] in java-interest

home help back first fref pref prev next nref lref last post

Re: Converting an int to a char?

daemon@ATHENA.MIT.EDU (Jonathan Payne)
Mon Oct 2 23:18:21 1995

Date: Mon, 2 Oct 1995 09:19:50 -0700
From: jpayne@starwave.com (Jonathan Payne)
To: pat@icon-stl.net
Cc: begenisi@oswego.Oswego.EDU, java-interest@java.sun.com
In-Reply-To: <Pine.SOL.3.91.951002011517.4939G-100000@flatland> (message from Pat Niemeyer on Mon, 2 Oct 1995 01:16:36 -0500 (CDT))

> Date: Mon, 2 Oct 1995 01:16:36 -0500 (CDT)
> From: Pat Niemeyer <pat@icon-stl.net>
> X-Sender: pat@flatland
> Cc: java-interest@java.sun.com
> Mime-Version: 1.0
> Sender: owner-java-interest@java.sun.com
> Precedence: bulk
> X-Info: To unsubscribe, send 'unsubscribe' to java-interest-request@java.sun.com
> Content-Type: TEXT/PLAIN; charset=US-ASCII
> Content-Length: 622
> 
> 
> 
> On Sun, 1 Oct 1995, John M Begenisich wrote:
> 
> > I'm trying to convert an int to a character.  This is what I basically 
> > have:
> > 
> >   String line = new String();
> >   int dum=0;
> >   while ((dum=(int)data.read())!=-1) {
> >     b=(char)dum;
> >     line+=(char)b;
> >     ...blah blah blah...
> >   }
> >   System.out.print(line);

You can get what you want here by changing:

	line += (char) b

to 

	line += new Character(b)

But neither of these is really all that appealing, since you get N
copies of the string, where N is the number of characters in the file
you're reading.

A better way to do this is:

    StringBuffer buf = new StringBuffer(data.avilable());
    int c;

    while ((c = data.read()) != -1)
	    buf.appendChar(c);

Even this is not so great because the appendChar() routine is a little
bit expensive because it checks to see whether the StringBuffer has
been made into a String anywhere yet (by saying buf.toString()), in
which case it makes a copy of itself so that the String which has been
made (and is supposed to be read-only) is not modified by subsequent
appends or inserts.

But this is certainly faster than what you were doing.

-- 
Jonathan Payne				jpayne@starwave.com
ESPNET SportsZone			http://starwave.com/people/jpayne/
Starwave Corporation
-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post