I have a string which I want to convert into bytes (using the encoding.getbytes() function properly) and then the bytes that result from the conversion, add them to a List.
How can I do this? I've thought about doing a for and converting each character in the string and add it one by one to the list but I would like to know if there is a more efficient way of doing this.
Can't you just convert the GetBytes array to a list?
List<byte> byteList = Encoding.Default.GetBytes(inputString).ToList();
Or pass the array to List's constructor:
List<byte> byteList = new List<Byte>(Encoding.ASCII.GetBytes(str));
class Program
{
static void Main(string[] args)
{
String str = "Kiran Bheemarti";
List<byte> bytes = Encoding.ASCII.GetBytes(str).ToList();
Console.Read();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With