Two Laptop Bag*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes converting a string to bytes 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 » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "converting a string to bytes" Watch "converting a string to bytes" New topic
Author

converting a string to bytes

Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
Hi,

I read in bytes, convert it to a string (for storing in cache). Therefore, when I write the string to the file, I need to convert it back to bytes. 2 questions:
1) is it best to store record data as an array of strings (in cache) or better to store as an array of bytes and convert to string only when outputting (for user)
2) what is the best way to convert a string back to bytes?

To convert byte to string I converted each byte to char
char d = (char) b;
and concatenated to give a string. But I need to know the best way to convert a string back to bytes.

Thanks
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
byte [] b = s.getBytes();

I suppose this in a way, but then I need to pad it - how to pad?

hmm..
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
I do not know the best ways of doing the conversion as Charsets and Character Encoding have always confused me

From the JDK API for String class, I presume we are expected to use the following:

String str = new String (buf, "US-ASCII");
byte[] buf= str.getBytes("US-ASCII");

The CharSet class API also says:

"Every instance of the Java virtual machine has a default charset, which may or may not be one of the standard charsets. The default charset is determined during virtual-machine startup and typically depends upon the locale and charset being used by the underlying operating system."

How do I find out the default charset used by my JDK?
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
Originally posted by Laura Williamson:
byte [] b = s.getBytes();

I suppose this in a way, but then I need to pad it - how to pad?

hmm..


Laura,
If no charset is used, then it takes the default charset of the JDK, which may or may not be US-ASCII! US-ASCII is mentioned in my instructions.

Padding with spaces or nulls is a different subject.I think the instructions say that the data is to be terminated with null terminator if the data size < field max size, but the suppplied db file contains no null terminator but only trailing spaces!
Richard Everhart
Ranch Hand

Joined: Nov 19, 2003
Posts: 54
Hi Laura,

Use System.arraycopy(). Get an array of bytes initialized to 0x0 (fancy way to say zero) the length of the field you want to create or update or whatever. Then just copy the bytes containing data into the empty bytes array following the API for System.arraycopy().

Rich
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
ok thank ya'all very much - i can work it out from there.

thanks
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
one more thing on this:

do i need to put a null terminator as the last elem of the array?
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
hi all:
String objects are immutable, cannot be reused. Parsing all the database into string objects is very time consuming, and waste resourse. The GC has to work very hard to clean up all these objects. I would go with that design, I would leave everything in bytes, then convery when needed.

A little of the topic. Can someone convince me why should one bring the database to the memory? Unless one has a memory farm..


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
faster to read?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: converting a string to bytes
 
Similar Threads
covert from byte to string and back to byte
long to byte array
Give a byte, get a char,,,
Bytes retrieval
long in a byte array