该设备的制造商告诉我他只支持此配置文件的单字节字符集.
在this wiki page我发现以下应该是单字节字符集:
> ISO 8859
> ISO / IEC 646(我找不到这个here)
>各种Microsoft / IBM代码页
但是当我在这些字符集上调用Encoding.GetMaxByteCount(1)时,它总是返回2.
我还尝试了其他各种编码(例如IBM437),但GetMaxByteCount也为其他字符集返回2.
方法Endoding.IsSingleByte似乎不可靠according to this
You should be careful in what your application does with the value for
IsSingleByte. An assumption of how an Encoding will proceed may still
be wrong. For example,Windows-1252 has a value of true for
Encoding.IsSingleByte,but Encoding.GetMaxByteCount(1) returns 2. This
is because the method considers potential leftover surrogates from a
prevIoUs decoder operation.
此外,方法Encoding.GetMaxByteCount也有一些相同的问题according to this
Note that GetMaxByteCount considers potential leftover surrogates from
a prevIoUs decoder operation. Because of the decoder,passing a value
of 1 to the method retrieves 2 for a single-byte encoding,such as
ASCII. Your application should use the IsSingleByte property if this
information is necessary.
因此,我不知道如何使用.
解决方法
然而!所有这一切只有在您直接使用编码器/解码器时才有意义.如果您正在使用Encoding上的操作,例如Encoding.GetBytes,那么所有这些都是从您身上抽象出来的,您将永远不需要知道.在这种情况下,只需使用IsSingleByte,你会没事的.