C# base64 编码/解码与对象序列化问题

发布于 2024-08-15 12:29:38 字数 1040 浏览 2 评论 0原文

我在我的项目(这是一个类)中使用 C# 中的序列化和反序列化。 它们被序列化并保存到 XML 文件中。加载项目时,一切顺利。

现在我尝试将序列化的项目编码为 Base64,然后保存文件,这也很顺利。文件的第一行(编码之前!)如下所示:

<?xml version="1.0" encoding="utf-8"?>
  <Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

当我解码文件时,在该行前面添加了一个 ?

?<?xml version="1.0" encoding="utf-8"?>
  <Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

我用于编码的代码:

byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
        string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
        return returnValue;

以及用于解码的代码:

byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
        string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
        return returnValue;

这可能是什么?我该如何解决这个问题?

I'm using serialization and deserialization in C# for my Project (which is a Class).
They are serialized and saved to an XML file. When loading the Project, all goes well.

Now I'm trying to encode the serialized Project to Base64 and then save the file, which goes well too. The first line of the file (before encoded!) looks like this:

<?xml version="1.0" encoding="utf-8"?>
  <Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

When I decode the file, there's a ? added in front of the line:

?<?xml version="1.0" encoding="utf-8"?>
  <Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

The code I use to encode:

byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
        string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
        return returnValue;

And the code for decoding:

byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
        string returnValue = System.Text.ASCIIEncoding.ASCII.GetString(encodedDataAsBytes);
        return returnValue;

What can this be and how can I fix this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

救星 2024-08-22 12:29:38

该文件将自身声明为 UTF-8 - 那么为什么要使用 ASCII 将其编码为二进制呢? UTF-8中有很多字符无法用ASCII表示。您是否必须首先在内存中保存文本形式的文件?为什么不直接将其作为二进制数据加载(例如File.ReadAllBytes)?

如果您确实需要以字符串开头,请使用Encoding.UTF-8(或Encoding.Unicode),尽管这可能会导致更大的字节数组),一切都应该没问题。该额外字符是字节顺序标记 - 无法用 ASCII 表示,因此使用“?”替换字符。

The file declares itself as UTF-8 - so why are you using ASCII to encode it into binary? There are many characters in UTF-8 which can't be represented in ASCII. Do you even have to have the file in text form in-memory to start with? Why not just load it as binary data to start with (e.g. File.ReadAllBytes)?

If you do need to start with a string, use Encoding.UTF-8 (or Encoding.Unicode, although that will probably result in a bigger byte array) and everything should be fine. That extra character is a byte order mark - which can't be represented in ASCII, hence the "?" replacement character.

﹉夏雨初晴づ 2024-08-22 12:29:38

猜测一下?表示字节顺序标记,它是无法用 ASCII 表示的字符。为什么不使用UTF-8编码?

byte[] toEncodeAsBytes = System.Text.Encoding.UTF8.GetBytes(toEncode);

At a guess ? represents the Byte-Order-Marker which is a character that cannot be represented in ASCII. Why are you not using the UTF-8 encoding?

byte[] toEncodeAsBytes = System.Text.Encoding.UTF8.GetBytes(toEncode);
吃不饱 2024-08-22 12:29:38

不必担心编码,也许只需使用 XmlWriter.Create(outPath),并将该 XmlWriter 传递给序列化代码。这将避免这个问题和其他问题(例如必须为大型对象图缓冲非常大的字符串)。有一个重载接受 XmlWriterSettings 以进行更精细的控制。

XmlWriter 被大多数 xml 代码接受。

Rather than having to worry about encoding, perhaps just use XmlWriter.Create(outPath), and pass that XmlWriter to your serialization code. That will avoid this issue, and other issues (such as having to buffer very large strings for large object graphs). There is an overload that accepts an XmlWriterSettings for finer control.

XmlWriter is accepted by most xml code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文