Louis Rhys
Louis Rhys

Reputation: 35647

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: 2570

Answers (4)

Sachin Shanbhag
Sachin Shanbhag

Reputation: 55529

Use GetBytes( )

Upvotes: 1

Itay Karo
Itay Karo

Reputation: 18296

Encoding.GetBytes method.

Upvotes: 0

Noffls
Noffls

Reputation: 5427

Encoding.UTF8.GetBytes("abcd");

Upvotes: 8

Thariama
Thariama

Reputation: 50840

Try

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

Upvotes: 2

Related Questions