Is there a way of casting a String to a byte[]?
my objective is to write that string to a file using the method
FileOutputStream.write(byte[] arrayofbytes) and i get an inconvertible
types when i cast from String data to (byte[]) data.
thanks
Thomas Weidenfeller - 10 Jan 2006 11:14 GMT
> Is there a way of casting a String to a byte[]?
> my objective is to write that string to a file using the method
> FileOutputStream.write(byte[] arrayofbytes) and i get an inconvertible
> types when i cast from String data to (byte[]) data.
> thanks
The correct way to write text data to a file is to use a Writer, not an
OutputStream.
But if you really want to mangle your text data pretty hard, read the
documentation of the String class.
/Thomas

Signature
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
Markus B. - 10 Jan 2006 11:15 GMT
Gordon Beaton - 10 Jan 2006 11:20 GMT
> Is there a way of casting a String to a byte[]?
> my objective is to
> write that string to a file using the method
> FileOutputStream.write(byte[] arrayofbytes) and i get an
> inconvertible types when i cast from String data to (byte[]) data.
Casting cannot be used to change an object from one type to another.
However if you had spent a few moments browsing the API docs for
String, you would have found methods that do what you need.
Note too that any kind of Writer (such as an OutputStreamWriter or a
FileWriter) let you write Strings directly, i.e. they do the
conversion on the fly, so you don't have to.
/gordon

Signature
[ do not email me copies of your followups ]
g o r d o n + n e w s @ b a l d e r 1 3 . s e
Chris Uppal - 10 Jan 2006 11:56 GMT
> Is there a way of casting a String to a byte[]?
> my objective is to write that string to a file using the method
> FileOutputStream.write(byte[] arrayofbytes) and i get an inconvertible
> types when i cast from String data to (byte[]) data.
Which bytes ?
Any given String has many different representations as binary data depending on
which character encoding you want to use. You /cannot/ convert a String to a
byte[] array without choosing an encoding. There are some APIs which /appear/
to allow you to do so (as you would have seen if you'd looked at the
documentation) but in fact they just choose the "system default" encoding for
you -- which is quite likely to be wrong for your purposes.
-- chris
Roedy Green - 10 Jan 2006 19:26 GMT
>Is there a way of casting a String to a byte[]?
not casting, but converting. See
http://mindprod.com/jgloss/conversion.html#BYTETOSTRING

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 10 Jan 2006 19:28 GMT
>Is there a way of casting a String to a byte[]?
>my objective is to write that string to a file using the method
>FileOutputStream.write(byte[] arrayofbytes) and i get an inconvertible
>types when i cast from String data to (byte[]) data.
A PrintWriter will do that conversion fro you automatically. See
http://mindprod.com/applets/fileio.html
And tell it you want to write encoded characters. It will generate
you a sample program.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.