Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 346
Wraper Avatar asked Nov 30 '25 06:11

Wraper


2 Answers

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)
like image 76
Shyju Avatar answered Dec 01 '25 19:12

Shyju


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);
like image 36
John Wu Avatar answered Dec 01 '25 19:12

John Wu