Convert string array to byte array

I want to create a file that reads a String array, but initially I only have a byte array, so first I want to convert it to a string array, since I can do this.

+7
c #
source share
1 answer

Try the following:

Byte[] bytes = System.Text.Encoding.UTF8.GetBytes(yourString); 

You may need to change this depending on the character encoding of your string - see System.Text.Encoding (in particular, its properties) for other encodings supported by this type.

If you need to go the other way (and convert Byte[] to String ) then do it (the character encoding tip still applies here):

 String yourString = System.Text.Encoding.UTF8.GetString(yourByteArray); 

It looks like your API you are using expects String[] , and calling GetString will provide you with only one instance of String , not an array. Perhaps something like this will work for your API call:

 String yourString = System.Text.Encoding.UTF8.GetString(yourByteArray); someType.ApiCall(new[] { yourString }); 
+16
source share

All Articles