Is there a static method for converting a string, int, etc. into a byte/byte array?
I already know about this method:
public static byte[] FromString(string Input)
{
ASCIIEncoding ByteEncoder = new ASCIIEncoding();
return ByteEncoder.GetBytes(Input);
}
But i was wondering if there is anything better. Would it be a problem if I made a static instance of the ASCII encoder to use?
Also – in terms of performance – how does the ASCII conversion method above compare with doing it via a for loop or other such methods that are multi-line solutions?
Also – when should I use ASCII vs. Unicode?
William
There’s no static method. There are, however, static instances of
Encoding.System.Text.Encoding.ASCII.GetBytes().In terms of performance, you should trust the BCL. It’s been written carefully so you should expect near-optimal code, and certainly better handling of all the edge cases one does not always think about.