aspose file tools*
The moose likes Beginning Java and the fly likes how to convert byte [] to String? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how to convert byte [] to String?" Watch "how to convert byte [] to String?" New topic
Author

how to convert byte [] to String?

Sarone Thach
Ranch Hand

Joined: Jun 25, 2003
Posts: 89
Hi,
I'm stuck, can some please tell me how to convert a byte array into a string.

does not work funnily enough.
thanks in advanced.
Sarone
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24184
    
  34

Use
String string = new String(buffer);
This will convert the bytes into characters according to the platform's default encoding; in the us-english locale, this is UTF-8, so one byte is one character.


[Jess in Action][AskingGoodQuestions]
Sarone Thach
Ranch Hand

Joined: Jun 25, 2003
Posts: 89
thanks that does work.
if I want an int out of the byte array do i need to convert it to String then to an Integer, then get the int value? is there a better way than this?
byte [] buffer = new byte[4];//pretend there is valid data here
String sIntValue = new String(buffer);
Integer IValue = new Integer(sIntValue);
int iValue = IValue.intValue();
Also if there is material that i can read to understand what is a byte and how it relates to char, int and String. I would greatly appricate this.
thanks.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
If you are looking to create an "int" based on the value of four "byte"-s, then you may have to start using the shift and boolean operators. This is getting into more advances stuff, but look at this example:

In the first part you have the numeric values and you want the new int to be the result of the four bytes. The "&" does a logical AND of the bits; the "<<" shifts them; and the "|" ORs the results. The end result is that you get a 1 in the first bit, 2 in the second bit, 3 in the third bit, and 4 in the fourth bit. Which gives you the number 16909060.
In the second example you have the String "1234" broken into four bytes and you are converting it, which leads to the number 1234.
So the answer to your question is, it depends on what you are trying to do.
With your second questions, if you google on "Java primitive" you'll find out a lot of information on Java primitives. "byte" is an 8-bit signed values (-128 to 127). "char" is an unsigned 16-bit value (0 to 65535?). "int" is a signed 32-bit value. String is an object (not a primitive) that happens to be based on Unicode.
Wayne L Johnson
Ranch Hand

Joined: Sep 03, 2003
Posts: 399
Better answer here: how to convert byte [] to String?
Sarone Thach
Ranch Hand

Joined: Jun 25, 2003
Posts: 89
this answer is very convenient for me, exactly what i want to do.
But I'm confused why you left shift the first byte by 24, 2nd by 16, 3rd by 8?
I would do it this way and end up with the right number:
number = ((buffer[3] & 0x000000FF) << 32) |
((buffer[2] & 0x000000FF) << 16) |
((buffer[1] & 0x000000FF) << 8) |
((buffer[0] & 0x000000FF));
is your 24 supposed to be 32? if it is then we are doing the reverse from one another.
I think its some thing to do with where the least significant bit is supposed to be. This makes me wonder how do i determine which end is the least significant bit. Is the least significant bit fixed or changeable??
Well that is my thought, can anyone confirm that or give me another explanation?
By the way I'm getting the byte array by another program, written by someone else ages ago. So It could be how they place the data in the byte array.
Sarone Thach
Ranch Hand

Joined: Jun 25, 2003
Posts: 89
In regards to the original question, if I had a byte array containing:
byte [] bytesArray = {49,50,49,0,0,0};
String sNumber = new String(bytesArray);

The numbers are ascii coded numbers. So sNumber is '121'.
You think that you will get a null terminated String, but when i set the sNumber to a JTextField,I can see 121 with alot of squares.
The lenght of the string is 6. I would have thought the lenght is supposed to be 3. Can someone explain what is going on here, is there a remedy to emit all the nulls from being displayed in the textfield.
Sarone Thach
Ranch Hand

Joined: Jun 25, 2003
Posts: 89
Well i found a remedy, but i still don't understand why it does not elminate all the nulls, when i convert it to the new String.
sNumber = new String(byteArray);
sNumber = sNumber.substring(0, sNumber.indexOf(0));
sNumber.trim();
[ November 22, 2003: Message edited by: Sarone Thach ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to convert byte [] to String?