views:

114

answers:

4

How do you get a byte array out of a string in C#? I would like to pass a string to this method.

+2  A: 

Try

public static byte[] StrToByteArray(string str)
{
    System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
    return encoding.GetBytes(str);
}
Thariama
Why create a new instance of UTF8Encoding when you can use Encoding.UTF8?
Jon Skeet
well, you are right
Thariama
+6  A: 
Encoding.UTF8.GetBytes("abcd");
Noffls
A: 

Encoding.GetBytes method.

Itay
+1  A: 

Use GetBytes( )

Sachin Shanbhag