40 lines
1.0 KiB
C#
40 lines
1.0 KiB
C#
using System;
|
|
using System.Text;
|
|
using XYLEM.Communication;
|
|
|
|
namespace XYLEM.Communication.ValueEncoders {
|
|
public static class StringEncoder_W1252 {
|
|
public static Encoding GetDefaultEncoding( ) {
|
|
return Encoding.GetEncoding( 1252 );
|
|
}
|
|
|
|
/// <summary>
|
|
/// Standard Windows 1252-encoding page.
|
|
/// </summary>
|
|
static Encoding s_Win1252 = GetDefaultEncoding( );
|
|
|
|
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_Win1252.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_Win1252.GetString( value );
|
|
var index = result.IndexOf( '\0' );
|
|
if (index >= 0)
|
|
return result.Substring( 0, result.IndexOf( '\0' ) );
|
|
else
|
|
return result;
|
|
}
|
|
}
|
|
}
|