C# 62进制类型实现
构造62进制类型SixtyTwoScale
完整代码
算法很简单。用法:
string s = string.Empty;
for (int i = 0; i < 50; i++)
{
s += "1";
}
Int64 l = Convert.ToInt64(s, 2);
Response.Write(l.ToString() + "<br/>");
Response.Write(l.ToString("x") + "<br/>");
Response.Write(Convert.ToString(l, 2) + "<br/>");
SixtyTwoScale ms = l;
Response.Write(ms.ToString());
Response.End();
结果:
1125899906842623 //10进制
3ffffffffffff //16进制
11111111111111111111111111111111111111111111111111 //2进制
59I69uGkv //62进制
by yurow at 2008-7-4.
http://www.cnblogs.com/birdshover/
完整代码
1 using System;
2
3 /// <summary>
4 /// C#的62进制实现
5 /// </summary>
6 public class SixtyTwoScale
7 {
8 /// <summary>
9 /// 62进制数
10 /// </summary>
11 private string value;
12 /// <summary>
13 /// 对应10进制长整数
14 /// </summary>
15 private Int64 value_long;
16 /// <summary>
17 /// 62进制数向10进制转换
18 /// </summary>
19 /// <param name="value"></param>
20 public SixtyTwoScale(string value)
21 {
22 this.value = value;
23
24 int length = value.Length;
25 Int64 result = 0;
26 for (int i = 0; i < length; i++)
27 {
28 Int64 val = (Int64)Math.Pow(62, (length - i - 1));
29 char c = value[i];
30 Int64 tmp = Convert(c);
31 result += tmp * val;
32 }
33 value_long = result;
34 }
35 /// <summary>
36 /// 10进制向62进制转换
37 /// </summary>
38 /// <param name="value_long"></param>
39 public SixtyTwoScale(Int64 value_long)
40 {
41 this.value_long = value_long;
if (value_long == 0) //补充
{
this.value = "0";
return;
}
42 Int64 result = value_long;
43 while (result > 0)
44 {
45 Int64 val = result % 62;
46 this.value = Convert((int)val).ToString() + this.value;
47 result = (Int64)(result / 62);
48 }
49 }
50 /// <summary>
51 /// 输出62进制
52 /// </summary>
53 /// <returns></returns>
54 public override string ToString()
55 {
56 return this.value;
57 }
58 /// <summary>
59 /// 输出10进制
60 /// </summary>
61 /// <returns></returns>
62 public Int64 ToInt64()
63 {
64 return value_long;
65 }
66
67 #region 字符数字相互转换
68 private char Convert(int val)
69 {
70 switch (val)
71 {
72 case 0:
73 return '0';
74 case 1:
75 return '1';
76 case 2:
77 return '2';
78 case 3:
79 return '3';
80 case 4:
81 return '4';
82 case 5:
83 return '5';
84 case 6:
85 return '6';
86 case 7:
87 return '7';
88 case 8:
89 return '8';
90 case 9:
91 return '9';
92
93 case 10:
94 return 'a';
95 case 11:
96 return 'b';
97 case 12:
98 return 'c';
99 case 13:
100 return 'd';
101 case 14:
102 return 'e';
103 case 15:
104 return 'f';
105 case 16:
106 return 'g';
107 case 17:
108 return 'h';
109 case 18:
110 return 'i';
111 case 19:
112 return 'j';
113 case 20:
114 return 'k';
115 case 21:
116 return 'l';
117 case 22:
118 return 'm';
119 case 23:
120 return 'n';
121 case 24:
122 return 'o';
123 case 25:
124 return 'p';
125 case 26:
126 return 'q';
127 case 27:
128 return 'r';
129 case 28:
130 return 's';
131 case 29:
132 return 't';
133 case 30:
134 return 'u';
135 case 31:
136 return 'v';
137 case 32:
138 return 'w';
139 case 33:
140 return 'x';
141 case 34:
142 return 'y';
143 case 35:
144 return 'z';
145
146 case 36:
147 return 'A';
148 case 37:
149 return 'B';
150 case 38:
151 return 'C';
152 case 39:
153 return 'D';
154 case 40:
155 return 'E';
156 case 41:
157 return 'F';
158 case 42:
159 return 'G';
160 case 43:
161 return 'H';
162 case 44:
163 return 'I';
164 case 45:
165 return 'J';
166 case 46:
167 return 'K';
168 case 47:
169 return 'L';
170 case 48:
171 return 'M';
172 case 49:
173 return 'N';
174 case 50:
175 return 'O';
176 case 51:
177 return 'P';
178 case 52:
179 return 'Q';
180 case 53:
181 return 'R';
182 case 54:
183 return 'S';
184 case 55:
185 return 'T';
186 case 56:
187 return 'U';
188 case 57:
189 return 'V';
190 case 58:
191 return 'W';
192 case 59:
193 return 'X';
194 case 60:
195 return 'Y';
196 case 61:
197 return 'Z';
198 }
199 return '0';
200 }
201
202 private int Convert(char c)
203 {
204 switch (c)
205 {
206 case '0':
207 return 0;
208 case '1':
209 return 1;
210 case '2':
211 return 2;
212 case '3':
213 return 3;
214 case '4':
215 return 4;
216 case '5':
217 return 5;
218 case '6':
219 return 6;
220 case '7':
221 return 7;
222 case '8':
223 return 8;
224 case '9':
225 return 9;
226
227 case 'a':
228 return 10;
229 case 'b':
230 return 11;
231 case 'c':
232 return 12;
233 case 'd':
234 return 13;
235 case 'e':
236 return 14;
237 case 'f':
238 return 15;
239 case 'g':
240 return 16;
241 case 'h':
242 return 17;
243 case 'i':
244 return 18;
245 case 'j':
246 return 19;
247 case 'k':
248 return 20;
249 case 'l':
250 return 21;
251 case 'm':
252 return 22;
253 case 'n':
254 return 23;
255 case 'o':
256 return 24;
257 case 'p':
258 return 25;
259 case 'q':
260 return 26;
261 case 'r':
262 return 27;
263 case 's':
264 return 28;
265 case 't':
266 return 29;
267 case 'u':
268 return 30;
269 case 'v':
270 return 31;
271 case 'w':
272 return 32;
273 case 'x':
274 return 33;
275 case 'y':
276 return 34;
277 case 'z':
278 return 35;
279
280 case 'A':
281 return 36;
282 case 'B':
283 return 37;
284 case 'C':
285 return 38;
286 case 'D':
287 return 39;
288 case 'E':
289 return 40;
290 case 'F':
291 return 41;
292 case 'G':
293 return 42;
294 case 'H':
295 return 43;
296 case 'I':
297 return 44;
298 case 'J':
299 return 45;
300 case 'K':
301 return 46;
302 case 'L':
303 return 47;
304 case 'M':
305 return 48;
306 case 'N':
307 return 49;
308 case 'O':
309 return 50;
310 case 'P':
311 return 51;
312 case 'Q':
313 return 52;
314 case 'R':
315 return 53;
316 case 'S':
317 return 54;
318 case 'T':
319 return 55;
320 case 'U':
321 return 56;
322 case 'V':
323 return 57;
324 case 'W':
325 return 58;
326 case 'X':
327 return 59;
328 case 'Y':
329 return 60;
330 case 'Z':
331 return 61;
332 }
333 return 0;
334 }
335 #endregion
336
337 #region 重写运算符
338 public static implicit operator SixtyTwoScale(string value)
339 {
340 return new SixtyTwoScale(value);
341 }
342
343 public static implicit operator SixtyTwoScale(Int64 value)
344 {
345 return new SixtyTwoScale(value);
346 }
347 #endregion
348 }
2
3 /// <summary>
4 /// C#的62进制实现
5 /// </summary>
6 public class SixtyTwoScale
7 {
8 /// <summary>
9 /// 62进制数
10 /// </summary>
11 private string value;
12 /// <summary>
13 /// 对应10进制长整数
14 /// </summary>
15 private Int64 value_long;
16 /// <summary>
17 /// 62进制数向10进制转换
18 /// </summary>
19 /// <param name="value"></param>
20 public SixtyTwoScale(string value)
21 {
22 this.value = value;
23
24 int length = value.Length;
25 Int64 result = 0;
26 for (int i = 0; i < length; i++)
27 {
28 Int64 val = (Int64)Math.Pow(62, (length - i - 1));
29 char c = value[i];
30 Int64 tmp = Convert(c);
31 result += tmp * val;
32 }
33 value_long = result;
34 }
35 /// <summary>
36 /// 10进制向62进制转换
37 /// </summary>
38 /// <param name="value_long"></param>
39 public SixtyTwoScale(Int64 value_long)
40 {
41 this.value_long = value_long;
if (value_long == 0) //补充
{
this.value = "0";
return;
}
42 Int64 result = value_long;
43 while (result > 0)
44 {
45 Int64 val = result % 62;
46 this.value = Convert((int)val).ToString() + this.value;
47 result = (Int64)(result / 62);
48 }
49 }
50 /// <summary>
51 /// 输出62进制
52 /// </summary>
53 /// <returns></returns>
54 public override string ToString()
55 {
56 return this.value;
57 }
58 /// <summary>
59 /// 输出10进制
60 /// </summary>
61 /// <returns></returns>
62 public Int64 ToInt64()
63 {
64 return value_long;
65 }
66
67 #region 字符数字相互转换
68 private char Convert(int val)
69 {
70 switch (val)
71 {
72 case 0:
73 return '0';
74 case 1:
75 return '1';
76 case 2:
77 return '2';
78 case 3:
79 return '3';
80 case 4:
81 return '4';
82 case 5:
83 return '5';
84 case 6:
85 return '6';
86 case 7:
87 return '7';
88 case 8:
89 return '8';
90 case 9:
91 return '9';
92
93 case 10:
94 return 'a';
95 case 11:
96 return 'b';
97 case 12:
98 return 'c';
99 case 13:
100 return 'd';
101 case 14:
102 return 'e';
103 case 15:
104 return 'f';
105 case 16:
106 return 'g';
107 case 17:
108 return 'h';
109 case 18:
110 return 'i';
111 case 19:
112 return 'j';
113 case 20:
114 return 'k';
115 case 21:
116 return 'l';
117 case 22:
118 return 'm';
119 case 23:
120 return 'n';
121 case 24:
122 return 'o';
123 case 25:
124 return 'p';
125 case 26:
126 return 'q';
127 case 27:
128 return 'r';
129 case 28:
130 return 's';
131 case 29:
132 return 't';
133 case 30:
134 return 'u';
135 case 31:
136 return 'v';
137 case 32:
138 return 'w';
139 case 33:
140 return 'x';
141 case 34:
142 return 'y';
143 case 35:
144 return 'z';
145
146 case 36:
147 return 'A';
148 case 37:
149 return 'B';
150 case 38:
151 return 'C';
152 case 39:
153 return 'D';
154 case 40:
155 return 'E';
156 case 41:
157 return 'F';
158 case 42:
159 return 'G';
160 case 43:
161 return 'H';
162 case 44:
163 return 'I';
164 case 45:
165 return 'J';
166 case 46:
167 return 'K';
168 case 47:
169 return 'L';
170 case 48:
171 return 'M';
172 case 49:
173 return 'N';
174 case 50:
175 return 'O';
176 case 51:
177 return 'P';
178 case 52:
179 return 'Q';
180 case 53:
181 return 'R';
182 case 54:
183 return 'S';
184 case 55:
185 return 'T';
186 case 56:
187 return 'U';
188 case 57:
189 return 'V';
190 case 58:
191 return 'W';
192 case 59:
193 return 'X';
194 case 60:
195 return 'Y';
196 case 61:
197 return 'Z';
198 }
199 return '0';
200 }
201
202 private int Convert(char c)
203 {
204 switch (c)
205 {
206 case '0':
207 return 0;
208 case '1':
209 return 1;
210 case '2':
211 return 2;
212 case '3':
213 return 3;
214 case '4':
215 return 4;
216 case '5':
217 return 5;
218 case '6':
219 return 6;
220 case '7':
221 return 7;
222 case '8':
223 return 8;
224 case '9':
225 return 9;
226
227 case 'a':
228 return 10;
229 case 'b':
230 return 11;
231 case 'c':
232 return 12;
233 case 'd':
234 return 13;
235 case 'e':
236 return 14;
237 case 'f':
238 return 15;
239 case 'g':
240 return 16;
241 case 'h':
242 return 17;
243 case 'i':
244 return 18;
245 case 'j':
246 return 19;
247 case 'k':
248 return 20;
249 case 'l':
250 return 21;
251 case 'm':
252 return 22;
253 case 'n':
254 return 23;
255 case 'o':
256 return 24;
257 case 'p':
258 return 25;
259 case 'q':
260 return 26;
261 case 'r':
262 return 27;
263 case 's':
264 return 28;
265 case 't':
266 return 29;
267 case 'u':
268 return 30;
269 case 'v':
270 return 31;
271 case 'w':
272 return 32;
273 case 'x':
274 return 33;
275 case 'y':
276 return 34;
277 case 'z':
278 return 35;
279
280 case 'A':
281 return 36;
282 case 'B':
283 return 37;
284 case 'C':
285 return 38;
286 case 'D':
287 return 39;
288 case 'E':
289 return 40;
290 case 'F':
291 return 41;
292 case 'G':
293 return 42;
294 case 'H':
295 return 43;
296 case 'I':
297 return 44;
298 case 'J':
299 return 45;
300 case 'K':
301 return 46;
302 case 'L':
303 return 47;
304 case 'M':
305 return 48;
306 case 'N':
307 return 49;
308 case 'O':
309 return 50;
310 case 'P':
311 return 51;
312 case 'Q':
313 return 52;
314 case 'R':
315 return 53;
316 case 'S':
317 return 54;
318 case 'T':
319 return 55;
320 case 'U':
321 return 56;
322 case 'V':
323 return 57;
324 case 'W':
325 return 58;
326 case 'X':
327 return 59;
328 case 'Y':
329 return 60;
330 case 'Z':
331 return 61;
332 }
333 return 0;
334 }
335 #endregion
336
337 #region 重写运算符
338 public static implicit operator SixtyTwoScale(string value)
339 {
340 return new SixtyTwoScale(value);
341 }
342
343 public static implicit operator SixtyTwoScale(Int64 value)
344 {
345 return new SixtyTwoScale(value);
346 }
347 #endregion
348 }
算法很简单。用法:
string s = string.Empty;
for (int i = 0; i < 50; i++)
{
s += "1";
}
Int64 l = Convert.ToInt64(s, 2);
Response.Write(l.ToString() + "<br/>");
Response.Write(l.ToString("x") + "<br/>");
Response.Write(Convert.ToString(l, 2) + "<br/>");
SixtyTwoScale ms = l;
Response.Write(ms.ToString());
Response.End();
结果:
1125899906842623 //10进制
3ffffffffffff //16进制
11111111111111111111111111111111111111111111111111 //2进制
59I69uGkv //62进制
by yurow at 2008-7-4.
http://www.cnblogs.com/birdshover/