-
Notifications
You must be signed in to change notification settings - Fork 0
/
ByteCharConverters.cs
104 lines (95 loc) · 3.28 KB
/
ByteCharConverters.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.Text;
namespace Be.Windows.Forms
{
/// <summary>
/// The interface for objects that can translate between characters and bytes.
/// </summary>
public interface IByteCharConverter
{
/// <summary>
/// Returns the character to display for the byte passed across.
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
char ToChar(byte b);
/// <summary>
/// Returns the byte to use when the character passed across is entered during editing.
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
byte ToByte(char c);
}
/// <summary>
/// The default <see cref="IByteCharConverter"/> implementation.
/// </summary>
public class DefaultByteCharConverter : IByteCharConverter
{
/// <summary>
/// Returns the character to display for the byte passed across.
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public virtual char ToChar(byte b)
{
return b > 0x1F && !(b > 0x7E && b < 0xA0) ? (char)b : '.';
}
/// <summary>
/// Returns the byte to use for the character passed across.
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public virtual byte ToByte(char c)
{
return (byte)c;
}
/// <summary>
/// Returns a description of the byte char provider.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "ANSI (Default)";
}
}
/// <summary>
/// A byte char provider that can translate bytes encoded in codepage 500 EBCDIC
/// </summary>
public class EbcdicByteCharProvider : IByteCharConverter
{
/// <summary>
/// The IBM EBCDIC code page 500 encoding. Note that this is not always supported by .NET,
/// the underlying platform has to provide support for it.
/// </summary>
private Encoding _ebcdicEncoding = Encoding.GetEncoding(500);
/// <summary>
/// Returns the EBCDIC character corresponding to the byte passed across.
/// </summary>
/// <param name="b"></param>
/// <returns></returns>
public virtual char ToChar(byte b)
{
string encoded = _ebcdicEncoding.GetString(new byte[] { b });
return encoded.Length > 0 ? encoded[0] : '.';
}
/// <summary>
/// Returns the byte corresponding to the EBCDIC character passed across.
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
public virtual byte ToByte(char c)
{
byte[] decoded = _ebcdicEncoding.GetBytes(new char[] { c });
return decoded.Length > 0 ? decoded[0] : (byte)0;
}
/// <summary>
/// Returns a description of the byte char provider.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return "EBCDIC (Code Page 500)";
}
}
}