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

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

问题描述

我有一个字节数组:
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编码读取

// 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);
    }
}

当该函数运行时, (i转换为字节数组)

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.

请注意,我使用WP7有限的API。

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

希望你们能帮忙。

谢谢!

...

如果我执行下面的代码,它几乎是正确的,唯一的错误是第5个到最后一个字节被分割。

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);


推荐答案

原始字节数组编码为UTF-8。因此,StreamReader用替换字符U + FFFD替换每个无效字节。当该字符被编码回UTF-8时,这导致字节序列 EF BF BD 。您不能从字符串构造原始字节值,因为信息完全丢失。

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天全站免登陆