CSharp examples for Language Basics:Data Type Cast
Converting a string to a byte array.
using System;/*w w w.j av a 2s.c om*/ using System.Text; // Need for type UTF8Encoding. using System.Globalization; // Need for cultures. class Program { static void Main(string[] args) { UTF8Encoding encoding = new UTF8Encoding(); Console.WriteLine("\nConvert a string to a byte array."); string custName = "book2s.com"; int numBytesInString = encoding.GetByteCount(custName); byte[] theBytes = encoding.GetBytes(custName); Console.WriteLine(theBytes.Length); foreach (byte b in theBytes) { Console.Write(b + " "); } } }