Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a string array to byte array

Tags:

c#

I want to make a file which reads String array but initially I am having only byte array so first I want to convert it into string array, so how can I do so.

like image 653
Raj Parmar Avatar asked Apr 17 '10 15:04

Raj Parmar


1 Answers

Try this:

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

You may need to change this up depending on the character encoding of your string - please see System.Text.Encoding (specifically its properties) for other encodings that are supported by this type.

If you need to go the other way (and convert a Byte[] to a String) then do this (The advice on character encoding still applies here as well):

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

It sounds like your the API you are using expects a String[] and a call to GetString will provide you with just a single 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 });
like image 166
Andrew Hare Avatar answered Oct 12 '22 15:10

Andrew Hare