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