简体   繁体   English

读取字节数组

[英]Reading byte Array

How can I read and convert to text file this byte[] value?如何读取此字节 [] 值并将其转换为文本文件?

来自调试器的屏幕

It is described in documentation like DataType="base64Binary" but I cant convert it to human readable format...它在 DataType="base64Binary" 等文档中进行了描述,但我无法将其转换为人类可读的格式...

I have tried:我试过了:

System.Text.Encoding.ASCII.GetString(gadsContent.Value)

and got并得到

?\b\0\0\0\0\0\0\0?ZY??Zr~?W5vp??\0?t??6@\b\t???pL\bm\b???8?????/^~????L?/Q@Q??????...
Convert.ToBase64String(gadsContent.Value)

and got并得到

H4sIAAAAAAAAANVaWZPiWnJ+51cQNRETdnCrtAAC9XT1jDZACAm0guRwTAhtCLSAFrQ4/OCxw3/AL15+hp8c9ttM/y8fUUBR273d99qecEd3FTonM09uJzM/0Z9/W4...

Update:更新:

Now I read that this has compression GZIP... How can I decompress and decode it ?现在我读到这有压缩 GZIP ......我如何解压和解码它?

You can use System.Text.Encoding class to convert between string and byte array.您可以使用System.Text.Encoding类在字符串和字节数组之间进行转换。 Based on the encoding of your input string/byte array, use the appropriate property from this class.根据输入字符串/字节数组的编码,使用此类中的适当属性。

Here is a sample for UTF8 encoding.这是UTF8编码的示例。

var someString = "Some text input";

//Convert the string to byte array
var byteArray= System.Text.Encoding.UTF8.GetBytes(someString);

//Convert a byte array to string
var stringFromByteArray = System.Text.Encoding.UTF8.GetString(byteArray);

You have many other encoding options in this class.此类中有许多其他编码选项。 Use the one as needed根据需要使用一个

  • UTF32 UTF32
  • UTF8 UTF8
  • Unicode统一码
  • ASCII ( Ascii is like really old ) ASCII ( Ascii 真的很旧)

If a file contains a base64-encoded binary array, you need to follow these steps:如果文件包含 base64 编码的二进制数组,则需要执行以下步骤:

  1. Open the file as text , using appropriate text encoding (ASCII, UTF8, etc).使用适当的文本编码(ASCII、UTF8 等)将文件作为文本打开。 You can ask .NET to try to detect the encoding if you want.如果需要,您可以要求 .NET 尝试检测编码。

  2. Read the text into a string.将文本读入字符串。

  3. Convert the string into a byte array using Convert.FromBase64String() .使用Convert.FromBase64String()将字符串转换为字节数组。

The shortest possible example I could come up with looks like this:我能想出的最短示例如下所示:

string text = System.IO.File.ReadAllText(filePath, Encoding.UTF8);
byte[] byteArray = Convert.FromBase64String(text);

If you don't know the encoding, you can omit the argument and hope .NET can detect it:如果您不知道编码,您可以省略参数并希望 .NET 可以检测到它:

string text = System.IO.File.ReadAllText(filePath);
byte[] byteArray = Convert.FromBase64String(text);

(these are not text bytes, this does not apply: Assuming those are text bytes, you need a text decoder: try (这些不是文本字节,这不适用:假设这些是文本字节,您需要一个文本解码器:尝试

System.Text.Encoding.ASCII.GetString(x.Value) System.Text.Encoding.ASCII.GetString(x.Value)

) )

Seeing as the first two bytes are 31, 139 that should mean it's GZip bytes.看到前两个字节是 31、139,这应该意味着它是 GZip 字节。

Stack Overflow has an article on decoding Gzip: Stack Overflow 有一篇关于解码 Gzip 的文章:

Unzipping a .gz file using C# 使用 C# 解压缩 .gz 文件

At this point I can't tell if it's a .tar.gz file (tarred first, then gzipped).此时我无法判断它是否是 .tar.gz 文件(先压缩,然后压缩)。 You'll have to un-gzip it to see what you get and then we can look at that.你必须解压缩它才能看到你得到了什么,然后我们可以看看。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM