Konvertiert einen string in ein byte[] oder ein byte[] in einen string. Es gibt jeweils eine Überladung. Wird kein Encoding angegeben, dann wird Encoding.Default verwendet.
/// <summary>
/// converts string to byte[]
/// </summary>
/// <param name="str">string (default encoding)</param>
/// <returns>byte[] from string</returns>
public static byte[] StringToByteArray(string str)
{
return StringToByteArray(str,Encoding.Default);
}
/// <summary>
/// converts string to byte[]
/// </summary>
/// <param name="str">string</param>
/// <param name="encoding">Encoding</param>
/// <returns>byte[] from string</returns>
public static byte[] StringToByteArray(string str, Encoding encoding)
{
return encoding.GetBytes(str);
}
/// <summary>
/// converts byte[] to string
/// </summary>
/// <param name="arr">byte[]</param>
/// <returns>string from byte[] (default encoding)</returns>
public static string ByteArrayToString(byte[] arr)
{
return ByteArrayToString(arr, Encoding.Default);
}
/// <summary>
/// converts byte[] to string
/// </summary>
/// <param name="arr">byte[]</param>
/// <param name="encoding">Encoding</param>
/// <returns>string from byte[]</returns>
public static string ByteArrayToString(byte[] arr, Encoding encoding)
{
return encoding.GetString(arr);
}
4 Kommentare zum Snippet