/Files/zerobug/Demisemiquaver.zip三十二进制类,合法字符0-9,A-V,遇V进位。目前支持加法。
----------------------------------------------------------------
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//**************************************************************************
2
* COPYRIGHT (C) 2007, SHANG GROUP. ALL RIGHTS RESERVED.
3
* FILENAME : Demisemiquaver.cs
4
* DESCRIPTION: 三十二进制类,可进行加法。
5
* 1.合法的字符包括0-9,A-V;
6
* 2.a-v字符会自动转为A-V;
7
* AUTHOR : ZEROBUG
8
* DATE : May 26, 2008
9
* VERSION : 0.1
10
*
11
* - MODIFIED HISTORY -
12
*
13
* 1.May 26, 2008 by Sun G.Q., 0.0 - 0.1
14
* - Created.
15
**************************************************************************/
16![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
17
using System;
18
using System.Collections.Generic;
19
using System.Text;
20![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
21
namespace Util
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
24
/// 自定义三十二进制,包括加法
25
/// </summary>
26
public class Demisemiquaver
27![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
28
private String _value;
29![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
30
public Demisemiquaver()
31![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
}
34
public Demisemiquaver(String v)
35![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
36
this.Value = v;
37
}
38
public String Value
39![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
40![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return _value; }
41
set
42![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
43
if (CheckString(value.ToUpper()))
44
_value = value.ToUpper();
45
else
46
throw (new ArgumentOutOfRangeException("Value", value, "Value must be assigned a string which includes the chars between '0-9' or 'A-V'."));
47
}
48
}
49![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
50![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
三十二进制加法#region 三十二进制加法
51![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
52
/// 三十二进制加法,自增1
53
/// </summary>
54
/// <param name="oldValue"></param>
55
/// <returns></returns>
56
public static String Add(String oldValue)
57![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
58
return Add(oldValue, "1");
59
}
60![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
61![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
62
/// 三十二进制加法
63
/// </summary>
64
/// <param name="oldValue"></param>
65
/// <param name="addValue"></param>
66
/// <returns></returns>
67
public static String Add(String oldValue, String addValue)
68![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
69
//检查字符合法性
70
if (!CheckString(oldValue.ToUpper()))
71
throw (new ArgumentOutOfRangeException("oldValue", oldValue, "Value must be assigned a string which includes the chars between '0-9' or 'A-V'."));
72
if (!CheckString(addValue.ToUpper()))
73
throw (new ArgumentOutOfRangeException("addValue", addValue, "Value must be assigned a string which includes the chars between '0-9' or 'A-V'."));
74
75![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
76
Int32 len1 = oldValue.Length;
77
Int32 len2 = addValue.Length;
78
Int32 cr = 0;
79
Int32 g = 0;
80![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
String add = String.Empty;
82![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
83
Char[] olds = oldValue.ToUpper().ToCharArray();
84
Char[] adds = addValue.ToUpper().ToCharArray();
85![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
86
//以长字符串为主
87
if (len1 > len2)
88![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
89
//先与短字符串相加
90
for (Int32 i = len2 - 1; i >= 0; i--)
91![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
92
g = AddChar(Convert32To10((Int32)olds[i + len1 - len2]), Convert32To10((Int32)adds[i]), cr, out cr);
93
add = Convert10To32(g, true) + add;
94
}
95
//进位并把长字符串的高位连接上
96
Int32 j = len1 - len2 - 1;
97
while (j >= 0)
98![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
99
g = AddChar(Convert32To10((Int32)olds[j]), cr, out cr);
100
add = Convert10To32(g, true) + add;
101
j--;
102
}
103
}
104
else if (len2 > len1)
105![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
106
for (Int32 i = len1 - 1; i >= 0; i--)
107![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
108
g = AddChar(Convert32To10((Int32)adds[i + len2 - len1]), Convert32To10((Int32)olds[i]), cr, out cr);
109
add = Convert10To32(g, true) + add;
110
}
111
Int32 j = len2 - len1 - 1;
112
while (j >= 0)
113![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
114
g = AddChar(Convert32To10((Int32)adds[j]), cr, out cr);
115
add = Convert10To32(g, true) + add;
116
j--;
117
}
118
}
119
else if (len1 == len2)
120![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
121
for (Int32 i = len1 - 1; i >= 0; i--)
122![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
123
g = AddChar(Convert32To10((Int32)adds[i + len2 - len1]), Convert32To10((Int32)olds[i]), cr, out cr);
124
add = Convert10To32(g, true) + add;
125
}
126
127
//g = AddChar(Convert32To10((Int32)adds[0]), cr, out cr);
128
if (cr > 0)
129
add = Convert10To32(cr, true) + add;
130
131
}
132![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
133
return add;
134
}
135
#endregion
136![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
137![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
2,10进制转为32进制,合法字符包括'1-9','A-V'#region 2,10进制转为32进制,合法字符包括'1-9','A-V'
138![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
139
/// 十进制转换为32进制:先转换为2进制, 再按照5位划分,转换为32进制
140
/// </summary>
141
/// <param name="Dec"></param>
142
/// <returns></returns>
143
public static String Convert10To32(Int32 Dec)
144![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
145
String dt2 = Convert.ToString(Dec, 2);
146
String s32 = String.Empty;
147![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
148![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//////////////////////////////////////////////////////////////////////////
149
//如果2进制的长度是5的整数倍时,直接按5位划分计算,
150
//否则,先从高位(左起)取余数位,再按5位划分计算
151![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////////////////////////////////////////////////////////////////
152
Int32 iRest = dt2.Length % 5;
153![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
154
if (iRest == 0)
155![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
156
for (Int32 i = 0; i < dt2.Length / 5; i++)
157![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
158
s32 = s32 + Convert10To32(Convert.ToInt32((dt2.Substring(i * 5, 5)), 2), true);
159
}
160
}
161
else
162![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
163
s32 = s32 + Convert10To32(Convert.ToInt32((dt2.Substring(0, iRest)), 2), true);
164![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
165
dt2 = dt2.Substring(iRest, dt2.Length - iRest);
166![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
167
for (Int32 i = 0; i < dt2.Length / 5; i++)
168![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
169
s32 = s32 + Convert10To32(Convert.ToInt32((dt2.Substring(i * 5, 5)), 2), true);
170
}
171
}
172
return s32;
173
}
174![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
175![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
176
/// 二进制转换为32进制:按照5位划分,转换为32进制
177
/// </summary>
178
/// <param name="Dec"></param>
179
/// <returns></returns>
180
public static String Convert2To32(String s2)
181![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
182
String dt2 = s2;
183
String s32 = String.Empty;
184![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
185![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//////////////////////////////////////////////////////////////////////////
186
//如果2进制的长度是5的整数倍时,直接按5位划分计算,
187
//否则,先从高位(左起)取余数位,再按5位划分计算
188![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**/////////////////////////////////////////////////////////////////////////
189
Int32 iRest = dt2.Length % 5;
190![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
191
if (iRest == 0)
192![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
193
for (Int32 i = 0; i < dt2.Length / 5; i++)
194![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
195
s32 = s32 + Convert10To32(Convert.ToInt32((dt2.Substring(i * 5, 5)), 2), true);
196
}
197
}
198
else
199![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
200
s32 = s32 + Convert10To32(Convert.ToInt32((dt2.Substring(0, iRest)), 2), true);
201![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
202
dt2 = dt2.Substring(iRest, dt2.Length - iRest);
203![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
204
for (Int32 i = 0; i < dt2.Length / 5; i++)
205![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
206
s32 = s32 + Convert10To32(Convert.ToInt32((dt2.Substring(i * 5, 5)), 2), true);
207
}
208
}
209
return s32;
210
}
211![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
212![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
213
/// 将小于32的10进制数,转换为32进制
214
/// </summary>
215
/// <param name="s10"></param>
216
/// <returns></returns>
217
public static String Convert10To32(Int32 dec, Boolean IsLessThan32)
218![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
219![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
220
String s = String.Empty;
221
if (IsLessThan32)
222![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
223
String s10 = dec.ToString();
224
if (s10.Length == 1)
225![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
226
s = s10;
227![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
228
}
229
else if (Convert.ToInt32(s10) < 32)
230![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
231
s = Convert.ToString((Char)(64 + (dec - 9)));
232![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
233
}
234
}
235
return s;
236
}
237![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
238
#endregion
239![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
240![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
检查合法性,合法字符包括‘0-9’,‘A-V'#region 检查合法性,合法字符包括‘0-9’,‘A-V'
241![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
242
/// 检查字符是否合法,合法字符包括‘0-9’,‘A-V'
243
/// </summary>
244
/// <param name="c"></param>
245
/// <returns></returns>
246
public static Boolean CheckChar(Char c)
247![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
248
if ((Int32)c < 48 || ((Int32)c > 57 && (Int32)c < 65) || (Int32)c > 86)
249
return false;
250
else
251
return true;
252
}
253![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
254![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
255
/// 检查字符串的组成字符是否合法,合法字符包括‘0-9’,‘A-V'
256
/// </summary>
257
/// <param name="s"></param>
258
/// <returns></returns>
259
public static Boolean CheckString(String s)
260![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
261
Boolean IsOK = true;
262
Char[] cs = s.ToCharArray();
263
for (Int32 i = 0; i < s.Length; i++)
264![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
265
if (!CheckChar(cs[i]))
266![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
267
IsOK = false;
268
break;
269
}
270
}
271
return IsOK;
272
}
273
#endregion
274![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
275![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
字符相加#region 字符相加
276![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
277
/// 把两个字符相加,并输出进位的字符
278
/// </summary>
279
/// <param name="c1"></param>
280
/// <param name="c2"></param>
281
/// <param name="cr"></param>
282
/// <returns></returns>
283
public static Int32 AddChar(Int32 c1, Int32 c2, out Int32 cr)
284![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
285![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
286
Int32 a = c1 + c2;
287
if (a < 32)
288![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
289
cr = 0;
290
return a;
291
}
292
else
293![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
294
cr = 1;
295
return a-32;
296
}
297
}
298![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
299![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
300
/// 把三个字符相加,并输出进位的字符cr
301
/// </summary>
302
/// <param name="c1"></param>
303
/// <param name="c2"></param>
304
/// <param name="c3"></param>
305
/// <param name="cr">进位</param>
306
/// <returns></returns>
307
public static Int32 AddChar(Int32 c1, Int32 c2, Int32 c3, out Int32 cr)
308![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
309
//0-96
310
Int32 a = c1 + c2 + c3;
311
if (a < 32)
312![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
313
cr = 0;
314
return a;
315
}
316
else if (a < 64)
317![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
318
cr = 1;
319
return a - 32;
320
}
321
else
322![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
323
cr = 2;
324
return a - 64;
325
}
326
}
327
#endregion
328![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
329![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
三十二进制转为10,2进制#region 三十二进制转为10,2进制
330![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
331
/// 三十二进制的字符(值)转为10进制
332
/// </summary>
333
/// <param name="c"></param>
334
/// <returns></returns>
335
public static Int32 Convert32To10(Int32 c)
336![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
337![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
338
Int32 iSubtract = 0;
339
if (c > 47 && c < 58)
340![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
341
iSubtract = (c - 48);
342
}
343
else if (c > 64 && c < 87)
344![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
345
iSubtract = (c - 64 + 9);
346
}
347
return iSubtract;
348![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
349
}
350![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
351![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
352
/// 三十二进制的字符串转为10进制,转之前要确认为有效的10进制
353
/// </summary>
354
/// <param name="s"></param>
355
/// <returns></returns>
356
public static Int32 Convert32To10(String s)
357![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
358![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////////////////////////////////
359
Int32 ii = 0;
360
return ii;
361
}
362![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
363![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
364
/// 三十二进制的字符串转为2进制,转之前要确认为有效的2进制
365
/// </summary>
366
/// <param name="s"></param>
367
/// <returns></returns>
368
public static String Convert32To2(String s)
369![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
370![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**///////////////////////////////////////////
371
String ss = String.Empty;
372
return ss;
373
}
374
#endregion
375![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
376
}
377![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
378
}
379![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)