• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to convert byte array to string in java?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic