加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 为什么我们在Encoder.GetBytes方法中使用flush参数

发布时间:2020-12-15 08:18:27 所属栏目:百科 来源:网络整理
导读:这个 link解释了Encoder.GetBytes方法,并且还有一个名为flush的bool参数.冲洗的解释是: true if this encoder can flush its state at the end of the conversion; otherwise,false. To ensure correct termination of a sequence of blocks of encoded byt
这个 link解释了Encoder.GetBytes方法,并且还有一个名为flush的bool参数.冲洗的解释是:

true if this encoder can flush its
state at the end of the conversion;
otherwise,false. To ensure correct
termination of a sequence of blocks of
encoded bytes,the last call to
GetBytes can specify a value of true
for flush.

但是我不明白同花顺是做什么的,也许我喝醉了或者是某事:).请你详细解释一下.

解决方法

假设您通过套接字连接接收数据.您将收到一个长文本作为几个byte []块.

1个Unicode字符可能占用UTF-8流中的2个字节,并且它可以分割为2个字节的块.单独编码2个字节块(并连接字符串)会产生错误.

所以你只能在最后一个块上指定flush = true.当然,如果你只有1个街区,那么这也是最后一个.

提示:使用TextReader并让它为您处理此问题.

编辑

镜像问题(实际上是问:GetBytes)稍微难以解释.

使用flush = true与在GetBytes(…)之后使用Encoder.Reset()相同.它清除了编码器的“状态”,

including trailing characters at the end of the previous data block,such as an unmatched high surrogate

基本思路是相同的:当从字符串转换为字节块时,反之亦然,这些块不是独立的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读