见下面函数,是一个写入发送缓冲区队列的函数
/// summary
/// 发送缓存区
/// /summary
/// param name="content"/param
/// param name="bufferOffset"/param
/// param name="senderBuffer"/param
private void WriteSendBuffer(string content,int bufferOffset, byte[] sendBuffer)
{
int byteLength = Encoding.UTF8.GetByteCount(content);
if (byteLength = sendBuffer.Length)
{
byte[] sendBytes = Encoding.UTF8.GetBytes(content);
//中午就会错误的 Encoding.UTF8.GetBytes(content, 0, byteLength, senderBuffer, bufferOffset);
//以上换成数组拷贝就木问题了
Array.Copy(sendBytes, 0, sendBuffer, bufferOffset, byteLength); // 拷贝到数据包缓冲区
}
}
注意到UTF8的GetBytes,content里没中文一点问题都没有,如果是ASCII也木问题,但是如果用Unioncode且有中文就直接数组越界,错误信息是:
索引和计数必须引用该字符串内的位置
估计这是.Net里面的一个Bug,反正肯定是中文字节没处理好,有分析原理的么?
好久没写.net 写了点就报这个错,伤不起。
转载于:https://www.cnblogs.com/gnhao/archive/2013/01/12/2857418.html