Louis Rhys
Louis Rhys

Reputation: 35627

how do convert string to byte[] in C#

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

Upvotes: 8

Views: 2552

Answers (4)

Sachin Shanbhag
Sachin Shanbhag

Reputation: 55489

Use GetBytes( )

Upvotes: 1

Itay Karo
Itay Karo

Reputation: 18286

Encoding.GetBytes method.

Upvotes: 0

Noffls
Noffls

Reputation: 5427

Encoding.UTF8.GetBytes("abcd");

Upvotes: 8

Thariama
Thariama

Reputation: 50832

Try

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

Upvotes: 2

Related Questions