wood burning stoves*
The moose likes Beginning Java and the fly likes How can I create a byte array from a string? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How can I create a byte array from a string?" Watch "How can I create a byte array from a string?" New topic
Author

How can I create a byte array from a string?

Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
I have a string �abcdefg�. Can create a byte[] from this string?
Thanks.
Bruce

BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Use the getBytes() method of String. It returns a Byte array.

"JavaRanch, where the deer and the Certified play" - David O'Meara
James Swan
Ranch Hand

Joined: Jun 26, 2001
Posts: 403
If you look at the API for java.lang.String there is a method:
public byte[] getBytes()
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Thanks Cindy.
It works.
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Thanks James!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How can I create a byte array from a string?
 
Similar Threads
NX: URLyBird 1.1.3 Find criteria
Convert byte to its hex format?
can i transform the Byte array to a String ?
How to write back an updated record to db file?
Concatenate->Print OR System.out.print() ???