Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2006

Tip: Looking for answers? Try searching our database.

how to convert string to byte[]?

Thread view: 
beruno@gmail.com - 10 Jan 2006 11:05 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
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
stringvar.getBytes()
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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.