这个字节数组是怎么回事? [英] Whats going on with this byte array?

查看:25
本文介绍了这个字节数组是怎么回事?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字节数组:00 01 00 00 00 12 81 00 00 01 00 C8 00 00 00 00 00 08 5C 9F 4F A5 09 45 D4 CE

通过StreamReader使用UTF8编码

读取

//注意,我无法将这段代码更改为许多依赖于它的组件.使用 (StreamReader streamReader =新的 StreamReader(responseStream, Encoding.UTF8, false)){string streamData = streamReader.ReadToEnd();if (requestData.Callback != null){requestData.Callback(response, streamData);}}

当该函数运行时,我得到以下返回给我(我转换为字节数组)

00 01 00 00 00 12 EF BF BD 00 00 01 00 EF BF BD 00 00 00 00 00 08 5C EF BF BD 4F EF BF BD 09 45 EF BF BD

不知何故,我需要将返回给我的内容恢复为正确的编码和正确的字节数组,但我已经尝试了很多.

请注意,我正在使用 WP7 受限 API.

希望大家帮帮忙.

谢谢!

更新帮助...

如果我执行以下代码,几乎是正确的,唯一错误的是倒数第 5 个字节被拆分.

byte[] writeBuf1 = System.Text.Encoding.UTF8.GetBytes(data);string buf1string = System.Text.Encoding.BigEndianUnicode.GetString(writeBuf1, 0, writeBuf1.Length);byte[] writeBuf = System.Text.Encoding.BigEndianUnicode.GetBytes(buf1string);

解决方案

原始字节数组编码为 UTF-8.StreamReader 因此将每个无效字节替换为 替换字符 U+FFFD.当该字符被编码回 UTF-8 时,将产生字节序列 EF BF BD.您无法从字符串构造原始字节值,因为信息已完全丢失.

I have a byte array: 00 01 00 00 00 12 81 00 00 01 00 C8 00 00 00 00 00 08 5C 9F 4F A5 09 45 D4 CE

It is read via StreamReader using UTF8 encoding

// Note I can't change this code, to many component dependent on it.
using (StreamReader streamReader = 
    new StreamReader(responseStream, Encoding.UTF8, false))
{
    string streamData = streamReader.ReadToEnd();
    if (requestData.Callback != null)
    {
        requestData.Callback(response, streamData);
    }
}

When that function runs I get the following returned to me (i converted to a byte array)

00 01 00 00 00 12 EF BF BD 00 00 01 00 EF BF BD 00 00 00 00 00 08 5C EF BF BD 4F EF BF BD 09 45 EF BF BD

Somehow I need to take whats returned to me and get it back to the right encoding and the right byte array, but I've tried alot.

Please be aware, I'm working with WP7 limited API.

Hopefully you guys can help.

Thanks!

Update for help...

if I do the following code, it's almost right, only thing that is wrong is the 5th to last byte gets split out.

byte[] writeBuf1 = System.Text.Encoding.UTF8.GetBytes(data);
                    string buf1string = System.Text.Encoding.BigEndianUnicode.GetString(writeBuf1, 0, writeBuf1.Length);
                    byte[] writeBuf = System.Text.Encoding.BigEndianUnicode.GetBytes(buf1string);

解决方案

The original byte array is not encoded as UTF-8. The StreamReader therefore replaces each invalid byte with the replacement character U+FFFD. When that character gets encoded back to UTF-8, this results in the byte sequence EF BF BD. You cannot construct the original byte value from the string because the information is completely lost.

这篇关于这个字节数组是怎么回事?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆