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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
using System;
using System.Collections.Generic;
using System.Text;</div>
namespace MyText
{
//wicky.start
public class UnicodeEncoding
{
private bool _bigEndian;
public UnicodeEncoding()
{
_bigEndian = false;
}
public UnicodeEncoding(bool bigEndian)
{
_bigEndian = bigEndian;
}
#region GetBytes
public byte[] GetBytes(string s)
{
if (s == null)
{
throw new ArgumentNullException("s", ("ArgumentNull_String"));
}
char[] chars = s.ToCharArray();
return this.GetBytes(chars, 0, chars.Length);
}
public byte[] GetBytes(char[] chars, int index, int count)
{
byte[] bytes = new byte[this.GetByteCount(chars, index, count)];
this.GetBytes(chars, index, count, bytes, 0);
return bytes;
}
public int GetByteCount(char[] chars, int index, int count)
{
if (chars == null)
{
throw new ArgumentNullException("chars", ("ArgumentNull_Array"));
}
if ((index < 0) || (count < 0))
{
throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", ("ArgumentOutOfRange_NeedNonNegNum"));
}
if ((chars.Length - index) < count)
{
throw new ArgumentOutOfRangeException("chars", ("ArgumentOutOfRange_IndexCountBuffer"));
}
int num = count * 2;
if (num < 0)
{
throw new ArgumentOutOfRangeException("count", ("ArgumentOutOfRange_GetByteCountOverflow"));
}
return num;
}
public int GetBytes(char[] chars, int charIndex, int charCount, byte[] bytes, int byteIndex)
{
int count = charCount * 2;
if ((chars == null) || (bytes == null))
{
throw new ArgumentNullException((chars == null) ? "chars" : "bytes", ("ArgumentNull_Array"));
}
if ((charIndex < 0) || (charCount < 0))
{
throw new ArgumentOutOfRangeException((charIndex < 0) ? "charIndex" : "charCount", ("ArgumentOutOfRange_NeedNonNegNum"));
}
if ((chars.Length - charIndex) < charCount)
{
throw new ArgumentOutOfRangeException("chars", ("ArgumentOutOfRange_IndexCountBuffer"));
}
if ((byteIndex < 0) || (byteIndex > bytes.Length))
{
throw new ArgumentOutOfRangeException("byteIndex", ("ArgumentOutOfRange_Index"));
}
if ((bytes.Length - byteIndex) < count)
{
throw new ArgumentException(("Argument_ConversionOverflow"));
}
if (this._bigEndian)
{
int num2 = charIndex + charCount;
while (charIndex < num2)
{
char ch = chars[charIndex++];
bytes[byteIndex++] = (byte)(ch >> 8);
bytes[byteIndex++] = (byte)ch;
}
return count;
}
Buffer.BlockCopy(chars, charIndex * 2, bytes, byteIndex, count);
return count;
}
#endregion GetBytes
#region GetString
public string GetString(byte[] bytes, int index, int count)
{
return new string(this.GetChars(bytes, index, count));
}
public char[] GetChars(byte[] bytes, int index, int count)
{
char[] chars = new char[this.GetCharCount(bytes, index, count)];
this.GetChars(bytes, index, count, chars, 0);
return chars;
}
public int GetCharCount(byte[] bytes, int index, int count)
{
if (bytes == null)
{
throw new ArgumentNullException("bytes", ("ArgumentNull_Array"));
}
if ((index < 0) || (count < 0))
{
throw new ArgumentOutOfRangeException((index < 0) ? "index" : "count", ("ArgumentOutOfRange_NeedNonNegNum"));
}
if ((bytes.Length - index) < count)
{
throw new ArgumentOutOfRangeException("bytes", ("ArgumentOutOfRange_IndexCountBuffer"));
}
return (count / 2);
}
public int GetChars(byte[] bytes, int byteIndex, int byteCount, char[] chars, int charIndex)
{
int num = byteCount / 2;
if ((bytes == null) || (chars == null))
{
throw new ArgumentNullException((bytes == null) ? "bytes" : "chars", ("ArgumentNull_Array"));
}
if ((byteIndex < 0) || (byteCount < 0))
{
throw new ArgumentOutOfRangeException((byteIndex < 0) ? "byteIndex" : "byteCount", ("ArgumentOutOfRange_NeedNonNegNum"));
}
if ((bytes.Length - byteIndex) < byteCount)
{
throw new ArgumentOutOfRangeException("bytes", ("ArgumentOutOfRange_IndexCountBuffer"));
}
if ((charIndex < 0) || (charIndex > chars.Length))
{
throw new ArgumentOutOfRangeException("charIndex", ("ArgumentOutOfRange_Index"));
}
if ((chars.Length - charIndex) < num)
{
throw new ArgumentException(("Argument_ConversionOverflow"));
}
byteCount = num * 2;
if (this._bigEndian)
{
int num2 = byteIndex + byteCount;
while (byteIndex < num2)
{
int num3 = bytes[byteIndex++];
int num4 = bytes[byteIndex++];
chars[charIndex++] = (char)((num3 << 8) | num4);
}
return num;
}
Buffer.BlockCopy(bytes, byteIndex, chars, charIndex * 2, byteCount);
return num;
}
#endregion GetBytes
}//end of class
//wicky.end
} |