将二进制数据转换为 Windows-1252 编码是否可能导致数据丢失?

发布于 2024-12-31 23:51:56 字数 334 浏览 1 评论 0原文

据我所知,将二进制数据转换为文本格式的最佳方法是使用 Base64 编码。 UTF-8 可能会导致丢失。但当我对此进行调查时,我发现 Windows-1252 编码的设计似乎不会导致数据丢失。

我在 此处的博客文章

最后,我提供了一些为什么我仍然不会将二进制数据存储为 Windows-1252 字符串的原因。但我很好奇是否存在我没​​有考虑到的实际数据丢失情况。

I understand that the best way to convert binary data to a textual format is to use base64 encoding. UTF-8 can result in lossiness. But as I was investigating this, I found that Windows-1252 encoding does not seem to result in data loss by way of its design.

I provide a lot more context in my blog post here.

At the end, I provide some reasons why I still wouldn't store binary data as a Windows-1252 string. But I'm curious if there's an actual data-loss scenario there I hadn't considered.

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

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

发布评论

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

评论(2

囚你心 2025-01-07 23:51:56

不应将二进制数据放入字符串中,因为二进制数据可以包含值低于 32 的字节。
这与字符串的编码无关。

我不确定你从哪里得到“UTF-8 是有损的,但 CP1252 不是”的信息。但我不确定我想知道。

You should NOT put binary data in a string, because binary data can contain bytes with values below 32.
This has nothing to do with the encoding of the string.

And I'm not sure where you got the "UTF-8 is lossy, but CP1252 is not" from. But I'm not sure I want to know.

瑾兮 2025-01-07 23:51:56

实际上,如果您认为您没有将二进制数据转换为CP1252,而是在C#中转换,那么这个问题就更好地考虑了>二进制数据CP1252UTF-16,所以问题是CP1252 -> UTF-16 -> CP1252保证没有多态性突变。

.net 文本编码器最适合 UTF-16 -> CP1252 充其量听起来很可疑,虽然测试可能没问题,但在很多情况下您都可以使用中间的 UTF-16 字符串执行任何操作仍然保证不会丢失数据,而且效率比字节数组低很多。

Really, the problem is better thought of if you consider that you aren't converting binary data to CP1252 but in C# you are converting binary data as CP1252 to UTF-16, so the question is would CP1252 -> UTF-16 -> CP1252 guarantee no polymorphic mutations.

The .net text encoder does a best fit on UTF-16 -> CP1252 that sounds iffy at best, while it may test okay, there aren't many scenarios in which you could do anything with that UTF-16 string in the middle that would still guarantee no data loss, and it's much less efficient than a byte array.

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