How to convert a string to a byte array in C#

The Encoding.GetBytes() method converts a string into a bytes array. The example below converts a string into a byte array in Ascii format and prints the converted bytes to the console.

string author = "Katy McClachlen";

// converts a C# string to a byte array

byte[] bytes = Encoding.ASCII.GetBytes(author);

foreach (byte b in bytes)

Jack Poorte
Jack Poorte
Last updated on Sep 30, 2023

Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.