[Mono-list] ASCII bytes to string?

edward.harvey.mono edward.harvey.mono at clevertrove.com
Fri Jan 11 14:20:26 UTC 2013


> From: mono-list-bounces at lists.ximian.com [mailto:mono-list-
> bounces at lists.ximian.com] On Behalf Of mickeyf
> 
> I am reading bytes from hardware device as a stream, as abbreviated here:
> 
> 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 (?).

I'm not completely sure what your question was, but it seems to be just some general confusion about strings and characters?

It's not like the old days - when we could just assume a string was actually an array of chars, and every char was the same size.  Depending on the encoding and the individual char, each character may be a different size, from 1 to 4 bytes, but typically 2.  

When you're reading a byte array from your device and converting to a string, each byte gets translated separately to a char, and in this case, apparently ends up being typically 2 bytes per character.  But that would be different, if only the byte values you read were different.  Some of them would become a 1 byte char, and some become up to 4 bytes char.

The length of a string is the number of chars, not the number of bytes.  I don't know how you find the number of bytes.



More information about the Mono-list mailing list