how do convert string to byte[] in C#

2019-01-27 19:44发布

问题:

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

回答1:

Encoding.UTF8.GetBytes("abcd");


回答2:

Try

public static byte[] StrToByteArray(string str)
{
    System.Text.UTF8Encoding  encoding=new System.Text.UTF8Encoding();
    return encoding.GetBytes(str);
}


回答3:

Use GetBytes( )



回答4:

Encoding.GetBytes method.