Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert string to byte[]

Tags:

c#

How do you convert string to byte[] in C#?

like image 987
n3wb13 Avatar asked Dec 16 '22 15:12

n3wb13


2 Answers

using byte[] data = Encoding.UTF8.GetBytes(myString);

like image 80
Teoman Soygul Avatar answered Jan 02 '23 12:01

Teoman Soygul


Note that .NET strings are encoded as Unicode (UTF-16):

byte[] bytes = Encoding.Unicode.GetBytes("a string");
like image 23
Mark Cidade Avatar answered Jan 02 '23 10:01

Mark Cidade