• 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
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Converting byte[] into a string

 
Ranch Hand
Posts: 681
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a byte[] and I am trying to check to see what data it contains.

checkData(byte [] data)
{
if(data != null && data.length > 0)
{
String dataString = new String(data);
System.out.println("data "+dataString);
}
}

This snippet of code causes a error exception

The code is called via a config so the error is not the actual error but a config error.

What I need to know is what is the best way to convert byte [] into a string.

Thansk for any help Tony
 
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
How about catching the exception right there and letting us know what it is? The problem is probably that there is a value in your byte array which can't be converted to the default character encoding.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should be careful about converting bytes into Strings. This may cause problems when it comes to internationalization and localizations. The conversion needs to take account regional settings and locales.


Originally posted by Tony Evans:
I have a byte[] and I am trying to check to see what data it contains.

checkData(byte [] data)
{
if(data != null && data.length > 0)
{
String dataString = new String(data);
System.out.println("data "+dataString);
}
}

This snippet of code causes a error exception

The code is called via a config so the error is not the actual error but a config error.

What I need to know is what is the best way to convert byte [] into a string.

Thansk for any help Tony

reply
    Bookmark Topic Watch Topic
  • New Topic