• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to convert byte array to string in java?

 
sam mon
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i have a code is

byte[] byteArry = (byte[]) pageInfo.getByteObject();

Then how can i convert this byteArry variable to String so i can display it ? anyone can give me example to handle it ?

Note: byteArry is a very large size (actually it is a text/html file). so i would like to handle it using stream class. any idea ?
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
go through this if you want it converted to a stream before displaying

http://java.sun.com/j2se/1.4.2/docs/api/java/io/ByteArrayInputStream.html

to convert directly to String, you can always use

String str = new String(byte[] byteArray);
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree
Just wanted to add that "new String(byte[])" would use the system's default encoding. If you know the bytes are coming for a source with different encoding, you might use another constructor:
new String(byte[] bytes, String charset).
 
Eung maeng
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I also tried to convert from Bytes to String with new String(Byte[] ss).
however, I can see the string value like '245678' rather than showed strange charcters on eclipse's console.

I convert to bytes from string like '245678' when passing to external class and get the byte value as same.
Then, I am trying to see the string value '245678' after convert to string from Bytes.

However, i can not see it. Does anyone know about what's happening? please reply for me.

cheers,
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul, might the encoding issue Sol mentioned be at play here? That is to say, are you certain that the bytes are in the platform default encoding?
 
Abhishek Jaiswal
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to do some bit transformations in a byte array and then store it in some value and then display the result but I am not able to do that. Like, if the byte array is named bArray and I have performed the operation-(bArray[1] & 0x80) >> 7) and if I store this value in a integer object, exception is thrown but if I display this result directly, I am able to do so. What is the problem?
 
Joe Ess
Bartender
Posts: 9361
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abhishek Jaiswal wrote:-(bArray[1] & 0x80) >> 7) and if I store this value in a integer object, exception is thrown


Note that this topic is about converting an array of characters into a String.
That said, how are you converting a byte array into an Integer? Or are you converting just bArray[1]? What exception are you getting?
Can you show us PostRealCode?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!