[Mono-list] ASCII bytes to string?

mickeyf mickey at thesweetoasis.com
Thu Jan 10 18:28:07 UTC 2013


I am reading bytes from hardware device as a stream, as abbreviated here:

byte[] buffFromDrv = new byte [BIG_ENOUGH];
bytesRead = thePipes.pRdDataFromDrv.Read(buffFromDrv, readPosition,
string s = System.Text.UTF8Encoding.UTF8.GetString(buffFromDrv); // also
tried with ASCIIEncooding

Console.WriteLine("len={0}, {1}", s.Length, s);

The string itself displays as expected, but shows a length of twice the
number of characters, as if String.Length is reporting the number of bytes
(UTF16) rather than the number of Unicode characters in the string.  

If I simply assign a literal string:

s = "abcdefg";

The length reported is as expected (7 in this case).

The documentation for string.length says "number of characters", not "number
of bytes", and this is what I have always seen. I'm quite sure I have done
this same thing successfully in Windows .NET with the behavior differing
from what I'm seeing now in mono. The C# (not mono) docs, if I am
understanding them correctly, say that GetString() should return a unicode
string, which apparently it does (?).

Have I missed something obvious here?

View this message in context: http://mono.1490590.n4.nabble.com/ASCII-bytes-to-string-tp4658076.html
Sent from the Mono - General mailing list archive at Nabble.com.

More information about the Mono-list mailing list