Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converting a string to bytes

 
Laura Williamson
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1066
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thank ya'all very much - i can work it out from there.

thanks
 
Laura Williamson
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
one more thing on this:

do i need to put a null terminator as the last elem of the array?
 
Hanna Habashy
Ranch Hand
Posts: 532
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Laura Williamson
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
faster to read?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic