wood burning stoves 2.0*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes How to convert byte array into string array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "How to convert byte array into string array?" Watch "How to convert byte array into string array?" New topic
Author

How to convert byte array into string array?

Karthik Balasubramanian
Ranch Hand

Joined: Dec 13, 2002
Posts: 43
Hi,
I am working on the Bodgitt and Scarper assignment. I was trying to arite this function:
public String[] read(int recNo) throws RecordNotFoundException;
I created a byte array, where the recors values are read into. But when I look at the method definition its return type is array of string. I dont know how to convert byte[] into string[]...sorry for asking this basic question.please help me out guys...
Thanks
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
One of the things you had to do for the SCJP was to look at the Java 2 API.
So I suggest that you take 30 seconds (it took me 20) to find the solution...
But you will probably realize that what you are trying to do is not exactly what you need to do.
[ December 14, 2003: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Zak Nixon
Ranch Hand

Joined: Sep 27, 2003
Posts: 126
Karthik,
byte[] array = new byte[2];
String str = new String(array);
This is how I converted an arbitrary array of bytes to a String.
Zak Nixon
SCJP 1.4
:roll:
Vishwa Kumba
Ranch Hand

Joined: Aug 27, 2003
Posts: 1064
The return type : Is it String or String[]?
Zak Nixon
Ranch Hand

Joined: Sep 27, 2003
Posts: 126
The return type is a String.
You will have to perform the String s = new String(byteArray); for
every record field you have and then build a String[] after all
of the fields have been read in.
Hope it helps.
Zak
Philippe Maquet
Bartender

Joined: Jun 02, 2003
Posts: 1872
Hi Karthik,
If you want to take some specific Charset into account (instead of the platform default one), you may use the String constructor which accepts a charset name as second parameter.
That's if your input is a byte array. In case your input is a ByteBuffer, have a look at Charset.decode().
Best,
Phil.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11481
    
  94

Hi Vish,

Originally posted by Karthik Balasubramanian:
public String[] read(int recNo) throws RecordNotFoundException;
Originally posted by Vish Kumar:
The return type : Is it String or String[]?

The return type is String[]
This method reads a record and returns an array of fields which make up that record - each field is a String, therefore it is an array of Strings.
You might choose to read the entire record as one byte array, and then break that byte array into each individual field and convert it to a String. Alternatively you might choose to read each individual field as a byte array, and convert that to a String.
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Karthik Balasubramanian
Ranch Hand

Joined: Dec 13, 2002
Posts: 43
Thanks to you all guys. As you guys suggested, I converted them into string then copied into a atring array.
Thanks for your help.
Karthik
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to convert byte array into string array?