Convert ANSI characters to UTF-8 in Java?

This error is not caused by character encoding. It means the length of the UTF data is wrong.

This error is not caused by character encoding. It means the length of the UTF data is wrong. EDIT: Just realized this is a writing error, not reading error.

The UTF length is only 2 bytes so it can only hold 64K UTF-8 bytes. You are trying to writing 100K, it's not going to work. This limit is hardcoded and no way to get around this, if (utflen > 65535) throw new UTFDataFormatException( "encoded string too long: " + utflen + " bytes").

– n002213f Sep 23 '09 at 14:50 You have to show me your test cases. They are wrong. See my edits.

– ZZ Coder Sep 23 '09 at 14:52 I used the following code to generate the test string; StringBuffer sb2 = new StringBuffer(); for (int i=0; I You just can't write long strings using writeUTF(). Write it your own way with a 4 byte length header. – ZZ Coder Sep 23 '09 at 15:28 @ZZ_Coder - noted, thanks – n002213f Sep 23 '09 at 4:44.

Byte asciiBytes = ...; String unicode = new String(asciiBytes, "US-ASCII"); byte utfBytes = unicode. GetBytes("UTF-8").

It seems I misread the original question regarding ASCII vs. ANSI, and with the latest question edits, my answer is not really relevant. – iammichael Sep 24 '09 at 13:42.

There are lots of different character encodings which all refer to "ANSI". The DOS codepage is 437 (without the drawing symbols). If you use codepage 850, this will work: String unicode = new String(bytes, "IBM850"); (where bytes is an array with the ANSI characters).

After that, you can convert this string into a byte array with any encoding using unicode. GetBytes(encoding). Windows often uses the codepage 1252 (use "windows-1252" for that).

Tried it but does not work, I get the same error. Is there a way to check the encoding in a string so that I can be sure its ANSI? – n002213f Sep 23 '09 at 15:02.

ZZ Coder already answered the question, but I have written a more detailed explanation and suggesting a workaround on this blog. Basically, the problem is in DataOutputStream, because it restricts the writeable String to 64KB. There are other possible workarounds to bystep the issue, some might work without breaking the actual binary data format one is using...

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions