压缩二进制数据、对其进行编码并转换为字符串的最佳顺序是什么?

发布于 2024-09-19 01:24:32 字数 745 浏览 10 评论 0 原文

我正在尝试将 gzip 压缩的二进制序列化对象存储到 Active Directory 的“扩展属性”中,更多信息 此处。该字段是一个 Unicode 字符串,其 oM 语法为 64。

我将二进制对象保存为 AD 的 Unicode 格式,如下所示:

byte[] bytes = ... // This is my blob 
System.Text.Encoding.Unicode.GetString(bytes); 

然后将其保存到扩展属性 #14。问题是,当我读取该值时,我没有取回整个字符串。

以下是实际保存到服务器的内容的屏幕截图: alt text

这是返回的屏幕截图: alt text

我猜测 \0 导致了问题,这可能意味着 null。我应该如何处理这个问题?除了 null 之外,我还应该转义其他字符吗?

I'm trying to store a gzipped binary serialized object into Active Directory's "Extension Attribute", more info here. This field is a Unicode string according to it's oM syntax of 64.

I'm saving the binary object into AD's Unicode format like this:

byte[] bytes = ... // This is my blob 
System.Text.Encoding.Unicode.GetString(bytes); 

I then save it to extension attribute #14. The issue is that when I read the value I don't get my entire string back.

Here is a screenshot of what is actually saved to the server:
alt text

Here is a screenshot of what comes back:
alt text

I am guessing that the \0 is causing the problem, and that probably means null. How should I handle this? Are there other chars I should also be escaping besides null?

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

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

发布评论

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

评论(1

注定孤独终老 2024-09-26 01:24:32

我假设您正在尝试将二进制数据放入字符串字段中。

简单地将数据从二进制转换为 Unicode 在某种程度上是一个坏主意(这就是您遇到的原因之一,但 Null (0) 并不是 Unicode 字符串编码中唯一可能给您带来问题的点。有其他控制字符,您可能有指向 Unicode 中保留的字符的字节对,等等)

我建议考虑使用 Base64。它就是为了这个目的而设计的。虽然这可能会妨碍您使用 gzip 进行压缩,但它应该可以解决您的问题。

您的代码将类似于:

byte[] bytes = ... // This is my blob 
System.Convert.ToBase64String(bytes); 

然后您使用:

System.Convert.ToBase64String(string); 

将数据以字节形式返回。

这绝对是比您正在做的更安全的方法。

I assume you're trying to put binary data into a string field.

Simply converting the data from binary to Unicode is somewhat of a bad idea (one of which is the reason you've encountered, but Null (0) isn't the only point in Unicode string encoding which may cause issues for you. There are other control characters, you might have byte pairs that point to characters that are reserved in Unicode, etc.)

I would recommend considering Base64 instead. It was designed for this exact purpose. While this probably hinders your compression efforts using gzip, it should solve your problem.

Your code will instead be something like:

byte[] bytes = ... // This is my blob 
System.Convert.ToBase64String(bytes); 

You then use:

System.Convert.ToBase64String(string); 

To get your data back as bytes.

This is definitely a safer approach than what you are doing.

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