*
The moose likes Beginning Java and the fly likes Convert byte to string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert byte to string" Watch "Convert byte to string" New topic
Author

Convert byte to string

Lawrence Keeney
Greenhorn

Joined: May 31, 2004
Posts: 17
I have been working on this problem all day and just can't seem to get it to work.

I have a program where I am inputing a character through an FTP connection. The character is located in the byte in[0]. I am trying to convert in[0] to a string using toString().

I have been able to get it to work using the String.valueOf, but the resulting string contains the ASCII representation of the character.

I have tried all combinations of converting the in[0] to a string variable z and can not get any of them to compile. Is there a simple way to do this?


Lawrence
Lawrence Keeney
Greenhorn

Joined: May 31, 2004
Posts: 17
I got this to work by using the following code;

The byte (in[]) is the character rectived.

[code}
y = (new String(in));
[/code]

I thought I would have to do it using toString().
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
You can use the "new String(byte[])" constructor:
Lawrence Keeney
Greenhorn

Joined: May 31, 2004
Posts: 17
Wayne,

Thanks for your quick response.

I guess that is what I essentially did with my code.

I still don't understand why toString() did not work.
Jon Poulton
Greenhorn

Joined: Jun 09, 2004
Posts: 27
The toString() method gives you a String representation of the object you call it on and the resultant String <i>should not be used programmatically</i>. What use is it? Use toString() for debugging.

toString() methods do not just "convert things into Strings", which is a common misconception. Also, you cannot call methods on primitives, only objects. If you were using an array of byte primitives, and tried to call toString() I dont think it would compile..
Jon Poulton
Greenhorn

Joined: Jun 09, 2004
Posts: 27
Oh yeah, There are some exceptions to the rule (as usual). You can use toString if you're doing something like:

Integer i = new Integer(5);
String five = i.toString();
// five.equals("5")

But be careful, this doesnt work for most object types. Try not to use toString() to do things in your programs other than debug.
Lawrence Keeney
Greenhorn

Joined: May 31, 2004
Posts: 17
Jon,

Thanks for the reply.

Most of the code I wrote using toString() did not compile, and when they did compile they did not work.

I am having a real hard time learning Java. I have written some quite complex programs in both Pascal and "C", but I seem to have a mental block when it comes to Java.
Jon Poulton
Greenhorn

Joined: Jun 09, 2004
Posts: 27
Dont worry about it. I came from C and had the same problem. It takes time to get used to thinking in terms of Objects, Classes, Interfaces etc.

My advice is to go and do as many of the SUN tutorials (trails) as you can (starting with the Learning the Language trail) and also download Bruce Eckels "Thinking in Java" which is both free, and a decent book on OO and Java as a language.

Good luck

Jon
Lawrence Keeney
Greenhorn

Joined: May 31, 2004
Posts: 17
Jon,

Thanks for the tips.

The Sun site looks so intimidating to me. I'll have to go back and take a look again.
Jon Poulton
Greenhorn

Joined: Jun 09, 2004
Posts: 27
Sun Java tutorials:
http://java.sun.com/docs/books/tutorial/index.html

Bruce Eckels TIJ:
http://www.mindview.net/Books/TIJ/

If you get stuck just post on a forum!
Jon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert byte to string
 
Similar Threads
problem base64 encoding the 3 hex bytes E2 80 A9
Random access reads and writes
Convert String Array to Byte Array
Filename in DBCS and JNI
Converting char[] to byte[]