Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you convert a string to a byte array in .NET?

I have a string that I need to convert to the equivalent array of bytes in .NET.

This ought to be easy, but I am having a brain cramp.

like image 613
JonStonecash Avatar asked Oct 27 '08 21:10

JonStonecash


1 Answers

You need to use an encoding (System.Text.Encoding) to tell .NET what you expect as the output. For example, in UTF-16 (= System.Text.Encoding.Unicode):

var result = System.Text.Encoding.Unicode.GetBytes(text); 
like image 50
Konrad Rudolph Avatar answered Oct 27 '22 08:10

Konrad Rudolph