30.一个自定义32进制类初稿

/Files/zerobug/Demisemiquaver.zip三十二进制类,合法字符0-9,A-V,遇V进位。目前支持加法。

----------------------------------------------------------------

 

ContractedBlock.gif ExpandedBlockStart.gif Code
  1ExpandedBlockStart.gifContractedBlock.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
 17using System;
 18using System.Collections.Generic;
 19using System.Text;
 20
 21namespace Util
 22ExpandedBlockStart.gifContractedBlock.gif{
 23ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
 24    /// 自定义三十二进制,包括加法
 25    /// </summary>

 26    public class Demisemiquaver
 27ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 28        private String _value;
 29
 30        public Demisemiquaver()
 31ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 32
 33        }

 34        public Demisemiquaver(String v)
 35ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 36            this.Value = v;
 37        }

 38        public String Value
 39ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 40ExpandedSubBlockStart.gifContractedSubBlock.gif            getreturn _value; }
 41            set 
 42ExpandedSubBlockStart.gifContractedSubBlock.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
 50ContractedSubBlock.gifExpandedSubBlockStart.gif        三十二进制加法#region 三十二进制加法
 51ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 52        /// 三十二进制加法,自增1
 53        /// </summary>
 54        /// <param name="oldValue"></param>
 55        /// <returns></returns>

 56        public static String Add(String oldValue)
 57ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 58            return Add(oldValue, "1");
 59        }

 60
 61ExpandedSubBlockStart.gifContractedSubBlock.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)
 68ExpandedSubBlockStart.gifContractedSubBlock.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
 76            Int32 len1 = oldValue.Length;
 77            Int32 len2 = addValue.Length;
 78            Int32 cr = 0;
 79            Int32 g = 0;
 80
 81            String add = String.Empty;
 82
 83            Char[] olds = oldValue.ToUpper().ToCharArray();
 84            Char[] adds = addValue.ToUpper().ToCharArray();
 85
 86            //以长字符串为主
 87            if (len1 > len2)
 88ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 89                //先与短字符串相加
 90                for (Int32 i = len2 - 1; i >= 0; i--)
 91ExpandedSubBlockStart.gifContractedSubBlock.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)
 98ExpandedSubBlockStart.gifContractedSubBlock.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)
105ExpandedSubBlockStart.gifContractedSubBlock.gif            {
106                for (Int32 i = len1 - 1; i >= 0; i--)
107ExpandedSubBlockStart.gifContractedSubBlock.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)
113ExpandedSubBlockStart.gifContractedSubBlock.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)
120ExpandedSubBlockStart.gifContractedSubBlock.gif            {
121                for (Int32 i = len1 - 1; i >= 0; i--)
122ExpandedSubBlockStart.gifContractedSubBlock.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
133            return add;
134        }

135        #endregion

136
137ContractedSubBlock.gifExpandedSubBlockStart.gif        2,10进制转为32进制,合法字符包括'1-9','A-V'#region 2,10进制转为32进制,合法字符包括'1-9','A-V'
138ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
139        /// 十进制转换为32进制:先转换为2进制, 再按照5位划分,转换为32进制
140        /// </summary>
141        /// <param name="Dec"></param>
142        /// <returns></returns>

143        public static String Convert10To32(Int32 Dec)
144ExpandedSubBlockStart.gifContractedSubBlock.gif        {
145            String dt2 = Convert.ToString(Dec, 2);
146            String s32 = String.Empty;
147
148ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//
149            //如果2进制的长度是5的整数倍时,直接按5位划分计算,
150            //否则,先从高位(左起)取余数位,再按5位划分计算
151ExpandedSubBlockStart.gifContractedSubBlock.gif            /**/
152            Int32 iRest = dt2.Length % 5;
153
154            if (iRest == 0)
155ExpandedSubBlockStart.gifContractedSubBlock.gif            {
156                for (Int32 i = 0; i < dt2.Length / 5; i++)
157ExpandedSubBlockStart.gifContractedSubBlock.gif                {
158                    s32 = s32 + Convert10To32(Convert.ToInt32((dt2.Substring(i * 55)), 2), true);
159                }

160            }

161            else
162ExpandedSubBlockStart.gifContractedSubBlock.gif            {
163                s32 = s32 + Convert10To32(Convert.ToInt32((dt2.Substring(0, iRest)), 2), true);
164
165                dt2 = dt2.Substring(iRest, dt2.Length - iRest);
166
167                for (Int32 i = 0; i < dt2.Length / 5; i++)
168ExpandedSubBlockStart.gifContractedSubBlock.gif                {
169                    s32 = s32 + Convert10To32(Convert.ToInt32((dt2.Substring(i * 55)), 2), true);
170                }

171            }

172            return s32;
173        }

174
175ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
176        /// 二进制转换为32进制:按照5位划分,转换为32进制
177        /// </summary>
178        /// <param name="Dec"></param>
179        /// <returns></returns>

180        public static String Convert2To32(String s2)
181ExpandedSubBlockStart.gifContractedSubBlock.gif        {
182            String dt2 = s2;
183            String s32 = String.Empty;
184
185ExpandedSubBlockStart.gifContractedSubBlock.gif            /**//
186            //如果2进制的长度是5的整数倍时,直接按5位划分计算,
187            //否则,先从高位(左起)取余数位,再按5位划分计算
188ExpandedSubBlockStart.gifContractedSubBlock.gif            /**/
189            Int32 iRest = dt2.Length % 5;
190
191            if (iRest == 0)
192ExpandedSubBlockStart.gifContractedSubBlock.gif            {
193                for (Int32 i = 0; i < dt2.Length / 5; i++)
194ExpandedSubBlockStart.gifContractedSubBlock.gif                {
195                    s32 = s32 + Convert10To32(Convert.ToInt32((dt2.Substring(i * 55)), 2), true);
196                }

197            }

198            else
199ExpandedSubBlockStart.gifContractedSubBlock.gif            {
200                s32 = s32 + Convert10To32(Convert.ToInt32((dt2.Substring(0, iRest)), 2), true);
201
202                dt2 = dt2.Substring(iRest, dt2.Length - iRest);
203
204                for (Int32 i = 0; i < dt2.Length / 5; i++)
205ExpandedSubBlockStart.gifContractedSubBlock.gif                {
206                    s32 = s32 + Convert10To32(Convert.ToInt32((dt2.Substring(i * 55)), 2), true);
207                }

208            }

209            return s32;
210        }

211
212ExpandedSubBlockStart.gifContractedSubBlock.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)
218ExpandedSubBlockStart.gifContractedSubBlock.gif        {
219
220            String s = String.Empty;
221            if (IsLessThan32)
222ExpandedSubBlockStart.gifContractedSubBlock.gif            {
223                String s10 = dec.ToString();
224                if (s10.Length == 1)
225ExpandedSubBlockStart.gifContractedSubBlock.gif                {
226                    s = s10;
227
228                }

229                else if (Convert.ToInt32(s10) < 32)
230ExpandedSubBlockStart.gifContractedSubBlock.gif                {
231                    s = Convert.ToString((Char)(64 + (dec - 9)));
232
233                }

234            }

235            return s;
236        }

237
238        #endregion

239
240ContractedSubBlock.gifExpandedSubBlockStart.gif        检查合法性,合法字符包括‘0-9’,‘A-V'#region 检查合法性,合法字符包括‘0-9’,‘A-V'
241ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
242        /// 检查字符是否合法,合法字符包括‘0-9’,‘A-V'
243        /// </summary>
244        /// <param name="c"></param>
245        /// <returns></returns>

246        public static Boolean CheckChar(Char c)
247ExpandedSubBlockStart.gifContractedSubBlock.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
254ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
255        /// 检查字符串的组成字符是否合法,合法字符包括‘0-9’,‘A-V'
256        /// </summary>
257        /// <param name="s"></param>
258        /// <returns></returns>

259        public static Boolean CheckString(String s)
260ExpandedSubBlockStart.gifContractedSubBlock.gif        {
261            Boolean IsOK = true;
262            Char[] cs = s.ToCharArray();
263            for (Int32 i = 0; i < s.Length; i++)
264ExpandedSubBlockStart.gifContractedSubBlock.gif            {
265                if (!CheckChar(cs[i]))
266ExpandedSubBlockStart.gifContractedSubBlock.gif                {
267                    IsOK = false;
268                    break;
269                }

270            }

271            return IsOK;
272        }

273        #endregion

274
275ContractedSubBlock.gifExpandedSubBlockStart.gif        字符相加#region 字符相加
276ExpandedSubBlockStart.gifContractedSubBlock.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)
284ExpandedSubBlockStart.gifContractedSubBlock.gif        {
285
286            Int32 a = c1 + c2;
287            if (a < 32)
288ExpandedSubBlockStart.gifContractedSubBlock.gif            {
289                cr = 0;
290                return a;
291            }

292            else
293ExpandedSubBlockStart.gifContractedSubBlock.gif            {
294                cr = 1;
295                return a-32;
296            }

297        }

298
299ExpandedSubBlockStart.gifContractedSubBlock.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)
308ExpandedSubBlockStart.gifContractedSubBlock.gif        {
309            //0-96
310            Int32 a = c1 + c2 + c3;
311            if (a < 32)
312ExpandedSubBlockStart.gifContractedSubBlock.gif            {
313                cr = 0;
314                return a;
315            }

316            else if (a < 64)
317ExpandedSubBlockStart.gifContractedSubBlock.gif            {
318                cr = 1;
319                return a - 32;
320            }

321            else
322ExpandedSubBlockStart.gifContractedSubBlock.gif            {
323                cr = 2;
324                return a - 64;
325            }

326        }

327        #endregion

328
329ContractedSubBlock.gifExpandedSubBlockStart.gif        三十二进制转为10,2进制#region 三十二进制转为10,2进制
330ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
331        /// 三十二进制的字符(值)转为10进制
332        /// </summary>
333        /// <param name="c"></param>
334        /// <returns></returns>

335        public static Int32 Convert32To10(Int32 c)
336ExpandedSubBlockStart.gifContractedSubBlock.gif        {
337
338            Int32 iSubtract = 0;
339            if (c > 47 && c < 58)
340ExpandedSubBlockStart.gifContractedSubBlock.gif            {
341                iSubtract = (c - 48);
342            }

343            else if (c > 64 && c < 87)
344ExpandedSubBlockStart.gifContractedSubBlock.gif            {
345                iSubtract = (c - 64 + 9);
346            }

347            return iSubtract;
348
349        }

350
351ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
352        /// 三十二进制的字符串转为10进制,转之前要确认为有效的10进制
353        /// </summary>
354        /// <param name="s"></param>
355        /// <returns></returns>

356        public static Int32 Convert32To10(String s)
357ExpandedSubBlockStart.gifContractedSubBlock.gif        {
358ExpandedSubBlockStart.gifContractedSubBlock.gif            /**///
359            Int32 ii = 0;
360            return ii;
361        }

362
363ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
364        /// 三十二进制的字符串转为2进制,转之前要确认为有效的2进制
365        /// </summary>
366        /// <param name="s"></param>
367        /// <returns></returns>

368        public static String Convert32To2(String s)
369ExpandedSubBlockStart.gifContractedSubBlock.gif        {
370ExpandedSubBlockStart.gifContractedSubBlock.gif            /**///
371            String ss = String.Empty;
372            return ss;
373        }

374        #endregion

375
376    }

377
378}

379

转载于:https://www.cnblogs.com/zerobug/archive/2008/11/26/1341167.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值