42 lines
1.1 KiB
C#
42 lines
1.1 KiB
C#
using System;
|
|
using System.Text;
|
|
using XYLEM.Communication;
|
|
|
|
namespace XYLEM.Communication.ValueEncoders
|
|
{
|
|
public static class StringEncoderUnicode
|
|
{
|
|
/// <summary>
|
|
/// Standard Windows 1252-encoding page.
|
|
/// </summary>
|
|
static Encoding s_Unicode = Encoding.BigEndianUnicode;
|
|
|
|
public static byte[] Encode(string value, int lenght)
|
|
{
|
|
if (value.Length > lenght)
|
|
{
|
|
throw new ArgumentException("value.Length is too long", "value");
|
|
}
|
|
|
|
var result = new byte[lenght];
|
|
s_Unicode.GetBytes(value).CopyTo(result, 0);
|
|
return result;
|
|
}
|
|
|
|
public static string Decode(byte[] value, int lenght)
|
|
{
|
|
if (value.Length > lenght)
|
|
{
|
|
throw new ArgumentException("value.Length wrong lenght", "value");
|
|
}
|
|
|
|
var result = s_Unicode.GetString(value);
|
|
var index = result.IndexOf('\0');
|
|
if (index >= 0)
|
|
return result.Substring(0, result.IndexOf('\0'));
|
|
else
|
|
return result;
|
|
}
|
|
}
|
|
}
|