Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting byte[] into a string

 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • 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
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • 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.
 
joseph edwards
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!