namespace S7.Net.Types
{
///
/// Contains the methods to convert from S7 Array of Chars (like a const char[N] C-String) to C# strings
///
public class String
{
///
/// Converts a string to of bytes, padded with 0-bytes if required.
///
/// The string to write to the PLC.
/// The amount of bytes reserved for the in the PLC.
public static byte[] ToByteArray(string value, int reservedLength)
{
var bytes = new byte[reservedLength];
if (value == null) return bytes;
var length = value.Length;
if (length == 0) return bytes;
if (length > reservedLength) length = reservedLength;
System.Text.Encoding.ASCII.GetBytes(value, 0, length, bytes, 0);
return bytes;
}
///
/// Converts S7 bytes to a string
///
///
///
public static string FromByteArray(byte[] bytes)
{
return System.Text.Encoding.ASCII.GetString(bytes);
}
}
}