Wraper
Wraper

Reputation: 39

Reading byte Array

How can I read and convert to text file this byte[] value?

screen from Debugger

It is described in documentation like DataType="base64Binary" but I cant convert it to human readable format...

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 ?

Upvotes: 3

Views: 25730

Answers (3)

Jacob Bruinsma
Jacob Bruinsma

Reputation: 1126

(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)

)

Seeing as the first two bytes are 31, 139 that should mean it's GZip bytes.

Stack Overflow has an article on decoding Gzip:

Unzipping a .gz file using C#

At this point I can't tell if it's a .tar.gz file (tarred first, then gzipped). You'll have to un-gzip it to see what you get and then we can look at that.

Upvotes: 0

Shyju
Shyju

Reputation: 218702

You can use System.Text.Encoding class to convert between string and byte array. Based on the encoding of your input string/byte array, use the appropriate property from this class.

Here is a sample for UTF8 encoding.

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
  • UTF8
  • Unicode
  • ASCII ( Ascii is like really old)

Upvotes: 3

John Wu
John Wu

Reputation: 52210

If a file contains a base64-encoded binary array, you need to follow these steps:

  1. Open the file as text, using appropriate text encoding (ASCII, UTF8, etc). You can ask .NET to try to detect the encoding if you want.

  2. Read the text into a string.

  3. Convert the string into a byte array using 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:

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

Upvotes: 1

Related Questions