3

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 ?

1
  • I think you are asking your question at too low of a level. Perhaps start with your web service calling code and even identify and provide information about the web service. Commented Oct 28, 2017 at 16:06

3 Answers 3

3

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)
Sign up to request clarification or add additional context in comments.

1 Comment

I have tried decoding in many method but something is wrong. String is unreadable for me ... like in Chinese language :)
1

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

Comments

0

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

1 Comment

I have tried everything method but something is wrong. String is unreadable... Like in Chinese language :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.