In Java, I have a String and I want to encode it as a byte array (in UTF8, or some other encoding). Alternately, I have a byte array (in some known encoding) and I want to convert it into a Java String. How do I do these conversions?
String original = "hello world";
byte[] utf8Bytes = original.getBytes("UTF8");
Convert from String to byte[]:
String s = "some text here";
byte[] b = s.getBytes("UTF-8");
Convert from byte[] to String:
byte[] b = {(byte) 99, (byte)97, (byte)116};
String s = new String(b, "US-ASCII");
You should, of course, use the correct encoding name. My examples used "US-ASCII" and "UTF-8", the two most common encodings.
You can convert directly via the String(byte[], String) constructor and getBytes(String) method. Java exposes available character sets via the Charset class. The JDK documentation lists supported encodings.
90% of the time, such conversions are performed on streams, so you'd use the Reader/Writer classes. You would not incrementally decode using the String methods on arbitrary byte streams - you would leave yourself open to bugs involving multibyte characters.
terribly late but i just encountered this issue and this is my fix:
private static String removeNonUtf8CompliantCharacters( final String inString ) {
if (null == inString ) return null;
byte[] byteArr = inString.getBytes();
for ( int i=0; i < byteArr.length; i++ ) {
byte ch= byteArr[i];
// remove any characters outside the valid UTF-8 range as well as all control characters
// except tabs and new lines
if ( !( (ch > 31 && ch < 253 ) || ch == '\t' || ch == '\n' || ch == '\r') ) {
byteArr[i]=' ';
}
}
return new String( byteArr );
}
If you refer the blog http://infomani.wordpress.com It may be good..
Here's a solution that avoids performing the Charset lookup for every conversion:
import java.nio.charset.Charset;
private final Charset UTF8_CHARSET = Charset.forName("UTF-8");
String decodeUTF8(byte[] bytes) {
return new String(bytes, UTF8_CHARSET);
}
byte[] encodeUTF8(String string) {
return string.getBytes(UTF8_CHARSET);
}
Hi,
I have used the following logic to eliminate ascii chars in my String, but it removes the double quotes in my string. Any idea how to keep the double quote.
text = text.replaceAll("\\\\n", "\n");
text = text.replaceAll("\\\\t", "\t");
text = text.replaceAll("\\\\r", "\r");
text = text.replaceAll("\\\\,", ",");
text = text.replaceAll("\\\\:", ":");
text = text.replaceAll("[^\\p{ASCII}]", "");
return text;
For ex:- if my String is : I rcvd "Achievers Award",.. i wanna 2 keep the double quote in this string...
Thanx