[Mono-list] Strange problems with System.Text.Encoding

neil.young neil.young at freenet.de
Fri Jul 4 16:16:14 EDT 2008


Hi group,

I don't want to bother you with the background of my problem, because I
could encapsulate the problematic things into a small console application,
which illustrates the core problem.

For some reasons I run into the follwoing exception: 

Unhandled Exception: System.ArgumentException: Arg_InsufficientSpace
Parameter name: chars
  at System.Text.UTF8Encoding.InternalGetChars (System.Byte* bytes, Int32
byteCount, System.Char* chars, Int32 charCount, System.UInt32& leftOverBits,
System.UInt32& leftOverCount, System.Object provider,
System.Text.DecoderFallbackBuffer& fallbackBuffer, System.Byte[]& bufferArg,
Boolean flush) [0x00000] 
  at System.Text.UTF8Encoding.InternalGetChars (System.Byte[] bytes, Int32
byteIndex, Int32 byteCount, System.Char[] chars, Int32 charIndex,
System.UInt32& leftOverBits, System.UInt32& leftOverCount, System.Object
provider, System.Text.DecoderFallbackBuffer& fallbackBuffer, System.Byte[]&
bufferArg, Boolean flush) [0x00000] 
  at System.Text.UTF8Encoding.GetChars (System.Byte[] bytes, Int32
byteIndex, Int32 byteCount, System.Char[] chars, Int32 charIndex) [0x00000] 
  at System.Text.Encoding.GetChars (System.Byte[] bytes, Int32 index, Int32
count) [0x00000] 
  at System.Text.Encoding.GetString (System.Byte[] bytes, Int32 index, Int32
count) [0x00000] 
  at System.Text.UTF8Encoding.GetString (System.Byte[] bytes, Int32 index,
Int32 count) [0x00000] 
  at System.Text.Encoding.GetString (System.Byte[] bytes) [0x00000] 
  at utf8.Program.Main (System.String[] args) [0x00000] 

while trying to convert a byte buffer to an UTF8 string. The following code
snippet, which runs PERFECTLY under .net 2.0 (Windows) is sufficient to
provoke the exception.

using System;
using System.Collections.Generic;
using System.Text;

namespace utf8 {
    class Program {
        static void Main(string[] args) {
            Random r = new Random();
            byte[] b = new byte[1024];
            r.NextBytes(b);
            for (int i = 0; i < 16; i++)
                Console.Write("{0:x} ", b[i]);
            Console.WriteLine();

            string sASCII = System.Text.Encoding.ASCII.GetString(b);
            Console.WriteLine("ASCII:   Converted successfully. Length: {0}
bytes, {1:x} {2:x} {3:x} {4:x} {5:x}", sASCII.Length, sASCII[0], sASCII[1],
sASCII[2], sASCII[3], sASCII[4]);

            string sUTF8 = System.Text.Encoding.UTF8.GetString(b);
            Console.WriteLine("UTF8:    Converted successfully. Length: {0}
bytes, {1:x} {2:x} {3:x} {4:x} {5:x}", sUTF8.Length, sUTF8[0], sUTF8[1],
sUTF8[2], sUTF8[3], sUTF8[4]);

            string sDefault = System.Text.Encoding.Default.GetString(b);
            Console.WriteLine("Default: Converted successfully. Length: {0}
bytes, {1:x} {2:x} {3:x} {4:x} {5:x}", sDefault.Length, sDefault[0],
sDefault[1], sDefault[2], sDefault[3], sDefault[4]);
         }
    }
}

It fills a buffer of 1024 bytes randomly and prints out the first 16 bytes
of the buffer. After that it tries to convert to ASCII, UTF8 and Default. I
have run the sample under VMWare, using the OpenSUSE 10.3/Mono 1.9.1 image
from the binary compiled under Windows. Under Windows the Default encoding
is somewhat like Windows 1252, whereas Default is UTF8 for Linux. So there
shall be no difference between Default and UTF8. The big question: Why does
the conversion except that strange?

Kind reagards. Any pointer welcome.



-- 
View this message in context: http://www.nabble.com/Strange-problems-with-System.Text.Encoding-tp18285467p18285467.html
Sent from the Mono - General mailing list archive at Nabble.com.



More information about the Mono-list mailing list