c# 将二进制数据读取到字符串中

发布于 2024-11-05 08:33:49 字数 350 浏览 0 评论 0原文

我已经看过很多如何将字节数组转换为字符串的示例,但是当我开始使用加密时,这给我带来了许多问题。出于好奇,有人知道直接将二进制数据读取到字符串上的方法吗?

执行此操作的 VB6 方法是:

Dim S as string
s = space$(lof(1))
Get #1,, s
close #1

如果没有,我已经看到了将字符串转换为字节数组、返回字符串(包括 base64、UTF8)的不同方法,最后 - 逐个字符并将其转换为字节。

我的目标很简单。我想将二进制数据检索到字符串中,加密该字符串,最后将所有字符串数据写回到新的二进制文件中。

您认为哪种方法是最快的方法?

I have seen plenty of examples of how to convert a byte array into a string, but this is causing me many problems down the road when I start to work with encryption. Just out of curiosity, does anyone know of a method to read binary data straight onto a string.

The VB6 method of doing this would be:

Dim S as string
s = space$(lof(1))
Get #1,, s
close #1

If not, I have seen different ways of converting string to byte array, back to string including base64, UTF8, and lastly - going through char by char and converting it into a byte.

My goal is simple. I want to retrieve binary data onto a string, encrypt this string, and lastly write all of the strings data back into a new binary file.

Which method would you say is the fastest way of doing this?

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

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

发布评论

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

评论(2

〆凄凉。 2024-11-12 08:33:49

如果您只想加密二进制数据,那么您应该完全避免使用字符串。如果您正在加密属于字符串中的文本信息,那么您应该关心编码(UTF-8 和类似的编码) - 尽管读取数据、将其放入字符串、加密字符串然后写出数据似乎是一个奇怪的要求。

If you simply want to encrypt binary data then you should avoid using a string altogether. If you are encrypting textual information that belongs in a string then you should be caring about encodings (UTF-8 and friends) - although reading in data, putting it into a string, encrypting the string and then writing out data seems an odd requirement.

時窥 2024-11-12 08:33:49

您声明要从 byte[]string 再到 byte[];首先,请注意编码(例如UTF-8)不是一个选项;仅当将字符串转换为二进制再返回字符串时才适用。区别很重要:它实际上需要是文本数据才能工作,并且任意byte[]不能被视为文本数据。出于类似的原因(不限于:代理等),这也排除了您的“直接写入字符串”。

将二进制编码为字符串的正确方法是使用 base-64、base-16 等; base-64 是这里最有效的通用编码。

然而;大多数加密代码适用于二进制,而不是文本。如果您有文本加密 API,在大多数情况下,它要做的第一件事就是使用 UTF-8 等编码将其转换为二进制。

所以:检查您是否缺少直接二进制 API 选项。

You state that you want to go from byte[] to string to byte[]; firstly, note that Encoding (such as UTF-8) is not an option; that only applies when going string to binary back to string. The difference is important: it needs to actually be textual data for that to work, and an arbitrary byte[] can not be treated as textual data. This also rules out your "direct write into a string", for similar reasons (not limited to: surrogates etc).

The correct way to encode binary to a string is with base-64, base-16, etc; base-64 is the most efficient general purpose encoding here.

However; most encryption code works with binary, not text. If you have a text encryption API, in most cases the first thing it is going to do is to turn it into binary using an encoding such as UTF-8.

So: check you aren't missing a direct binary API option.

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