How to save byte[] in c# application settings
Asked Answered
B

3

24

I am trying to save a byte array (byte[]) in c# application settings that is returned by Object List View.

Can anyone give me a solution on how to save byte array in c# application settings? or some trick on how to convert byte[] to something like a string then store, then retrieve and again convert it to byte array and give it back to object list view.

Bleed answered 8/9, 2012 at 11:2 Comment(0)
V
33

One of the most common ways to make a string from an array of bytes is encoding them in Base-64:

string encoded = System.Convert.ToBase64String(toEncodeAsBytes);

Use

byte[] bytes = System.Convert.FromBase64String(encoded);

to get your bytes back.

Velocipede answered 8/9, 2012 at 11:6 Comment(1)
My top favorite :)Stinker
W
4

The canonical way to do this is to convert the byte[] to a string via base64 and the other way round.

Willowwillowy answered 8/9, 2012 at 11:6 Comment(0)
F
3

By Different way you can convert Byte array to string and string to byte array. Like this :

1)

string asciiString = ASCIIEncoding.ASCII.GetString(byteArray);

byte[] byte = ASCIIEncoding.ASCII.GetBytes(asciiString);

2)

string base64String = System.Convert.ToBase64String(byteArray);

byte[] byte = System.Convert.FromBase64String(base64String);

3)

string utf8String = System.Text.Encoding.UTF8.GetString(byteArray);

byte[] byte = System.Text.Encoding.UTF8.GetBytes(utf8String);

you can also use System.Text.Encoding.BigEndianUnicode, System.Text.Encoding.Unicode, and System.Text.Encoding.UTF32 for converting Byte Array to string and string to Byte Array.

Hope, It should help you.

Folacin answered 8/9, 2012 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.