C# 进制之间的转换

  1 /// <summary>
  2         /// 十进制转换为二进制
  3         /// </summary>
  4         /// <param name="x"></param>
  5         /// <returns></returns>
  6         public static string DecToBin(string x)
  7         {
  8             string z = null;
  9             int X = Convert.ToInt32(x);
 10             int i = 0;
 11             long a, b = 0;
 12             while (X > 0)
 13             {
 14                 a = X%2;
 15                 X = X/2;
 16                 b = b + a*Pow(10, i);
 17                 i++;
 18             }
 19             z = Convert.ToString(b);
 20             return z;
 21         }
 22 
 23         /// <summary>
 24         /// 16进制转ASCII码
 25         /// </summary>
 26         /// <param name="hexString"></param>
 27         /// <returns></returns>
 28         public static string HexToAscii(string hexString)
 29         {
 30             StringBuilder sb = new StringBuilder();
 31             for (int i = 0; i <= hexString.Length - 2; i += 2)
 32             {
 33                 sb.Append(
 34                     Convert.ToString(
 35                         Convert.ToChar(Int32.Parse(hexString.Substring(i, 2),
 36                                                    System.Globalization.NumberStyles.HexNumber))));
 37             }
 38             return sb.ToString();
 39         }
 40 
 41         /// <summary>
 42         /// 十进制转换为八进制
 43         /// </summary>
 44         /// <param name="x"></param>
 45         /// <returns></returns>
 46         public static string DecToOtc(string x)
 47         {
 48             string z = null;
 49             int X = Convert.ToInt32(x);
 50             int i = 0;
 51             long a, b = 0;
 52             while (X > 0)
 53             {
 54                 a = X%8;
 55                 X = X/8;
 56                 b = b + a*Pow(10, i);
 57                 i++;
 58             }
 59             z = Convert.ToString(b);
 60             return z;
 61         }
 62 
 63         /// <summary>
 64         /// 十进制转换为十六进制
 65         /// </summary>
 66         /// <param name="x"></param>
 67         /// <returns></returns>
 68         public static string DecToHex(string x)
 69         {
 70             if (string.IsNullOrEmpty(x))
 71             {
 72                 return "0";
 73             }
 74             string z = null;
 75             int X = Convert.ToInt32(x);
 76             Stack a = new Stack();
 77             int i = 0;
 78             while (X > 0)
 79             {
 80                 a.Push(Convert.ToString(X%16));
 81                 X = X/16;
 82                 i++;
 83             }
 84             while (a.Count != 0)
 85                 z += ToHex(Convert.ToString(a.Pop()));
 86             if (string.IsNullOrEmpty(z))
 87             {
 88                 z = "0";
 89             }
 90             return z;
 91         }
 92 
 93         /// <summary>
 94         /// 二进制转换为十进制
 95         /// </summary>
 96         /// <param name="x"></param>
 97         /// <returns></returns>
 98         public static string BinToDec(string x)
 99         {
100             string z = null;
101             int X = Convert.ToInt32(x);
102             int i = 0;
103             long a, b = 0;
104             while (X > 0)
105             {
106                 a = X%10;
107                 X = X/10;
108                 b = b + a*Pow(2, i);
109                 i++;
110             }
111             z = Convert.ToString(b);
112             return z;
113         }
114 
115         /// <summary>
116         /// 二进制转换为十进制,定长转换
117         /// </summary>
118         /// <param name="x"></param>
119         /// <param name="iLength"></param>
120         /// <returns></returns>
121         public static string BinToDec(string x, short iLength)
122         {
123             StringBuilder sb = new StringBuilder();
124             int iCount = 0;
125 
126             iCount = x.Length/iLength;
127 
128             if (x.Length%iLength > 0)
129             {
130                 iCount += 1;
131             }
132 
133             int X = 0;
134 
135             for (int i = 0; i < iCount; i++)
136             {
137                 if ((i + 1)*iLength > x.Length)
138                 {
139                     X = Convert.ToInt32(x.Substring(i*iLength, (x.Length - iLength)));
140                 }
141                 else
142                 {
143                     X = Convert.ToInt32(x.Substring(i*iLength, iLength));
144                 }
145                 int j = 0;
146                 long a, b = 0;
147                 while (X > 0)
148                 {
149                     a = X%10;
150                     X = X/10;
151                     b = b + a*Pow(2, j);
152                     j++;
153                 }
154                 sb.AppendFormat("{0:D2}", b);
155             }
156             return sb.ToString();
157         }
158 
159         /// <summary>
160         /// 二进制转换为十六进制,定长转换
161         /// </summary>
162         /// <param name="x"></param>
163         /// <param name="iLength"></param>
164         /// <returns></returns>
165         public static string BinToHex(string x, short iLength)
166         {
167             StringBuilder sb = new StringBuilder();
168             int iCount = 0;
169 
170             iCount = x.Length/iLength;
171 
172             if (x.Length%iLength > 0)
173             {
174                 iCount += 1;
175             }
176 
177             int X = 0;
178 
179             for (int i = 0; i < iCount; i++)
180             {
181                 if ((i + 1)*iLength > x.Length)
182                 {
183                     X = Convert.ToInt32(x.Substring(i*iLength, (x.Length - iLength)));
184                 }
185                 else
186                 {
187                     X = Convert.ToInt32(x.Substring(i*iLength, iLength));
188                 }
189                 int j = 0;
190                 long a, b = 0;
191                 while (X > 0)
192                 {
193                     a = X%10;
194                     X = X/10;
195                     b = b + a*Pow(2, j);
196                     j++;
197                 }
198                 //前补0
199                 sb.Append(DecToHex(b.ToString()));
200             }
201             return sb.ToString();
202         }
203 
204         /// <summary>
205         /// 八进制转换为十进制
206         /// </summary>
207         /// <param name="x"></param>
208         /// <returns></returns>
209         public static string OctToDec(string x)
210         {
211             string z = null;
212             int X = Convert.ToInt32(x);
213             int i = 0;
214             long a, b = 0;
215             while (X > 0)
216             {
217                 a = X%10;
218                 X = X/10;
219                 b = b + a*Pow(8, i);
220                 i++;
221             }
222             z = Convert.ToString(b);
223             return z;
224         }
225 
226 
227         /// <summary>
228         /// 十六进制转换为十进制
229         /// </summary>
230         /// <param name="x"></param>
231         /// <returns></returns>
232         public static string HexToDec(string x)
233         {
234             if (string.IsNullOrEmpty(x))
235             {
236                 return "0";
237             }
238             string z = null;
239             Stack a = new Stack();
240             int i = 0, j = 0, l = x.Length;
241             long Tong = 0;
242             while (i < l)
243             {
244                 a.Push(ToDec(Convert.ToString(x[i])));
245                 i++;
246             }
247             while (a.Count != 0)
248             {
249                 Tong = Tong + Convert.ToInt64(a.Pop())*Pow(16, j);
250                 j++;
251             }
252             z = Convert.ToString(Tong);
253             return z;
254         }
255 
256         #endregion //Helperfunctions
257 
258         /// <summary>
259         /// 
260         /// </summary>
261         /// <param name="x"></param>
262         /// <param name="y"></param>
263         /// <returns></returns>
264         private static long Pow(long x, long y)
265         {
266             int i = 1;
267             long X = x;
268             if (y == 0)
269                 return 1;
270             while (i < y)
271             {
272                 x = x*X;
273                 i++;
274             }
275             return x;
276         }
277 
278         /// <summary>
279         /// 
280         /// </summary>
281         /// <param name="x"></param>
282         /// <returns></returns>
283         private static string ToDec(string x)
284         {
285             switch (x)
286             {
287                 case "A":
288                     return "10";
289                 case "B":
290                     return "11";
291                 case "C":
292                     return "12";
293                 case "D":
294                     return "13";
295                 case "E":
296                     return "14";
297                 case "F":
298                     return "15";
299                 default:
300                     return x;
301             }
302         }
303 
304         /// <summary>
305         /// 
306         /// </summary>
307         /// <param name="x"></param>
308         /// <returns></returns>
309         private static string ToHex(string x)
310         {
311             switch (x)
312             {
313                 case "10":
314                     return "A";
315                 case "11":
316                     return "B";
317                 case "12":
318                     return "C";
319                 case "13":
320                     return "D";
321                 case "14":
322                     return "E";
323                 case "15":
324                     return "F";
325                 default:
326                     return x;
327             }
328         }
329 
330         /// <summary>
331         /// 将16进制BYTE数组转换成16进制字符串
332         /// </summary>
333         /// <param name="bytes"></param>
334         /// <returns></returns>
335         public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF "
336         {
337             string hexString = string.Empty;
338             if (bytes != null)
339             {
340                 StringBuilder strB = new StringBuilder();
341 
342                 for (int i = 0; i < bytes.Length; i++)
343                 {
344                     strB.Append(bytes[i].ToString("X2"));
345                 }
346                 hexString = strB.ToString();
347             }
348             return hexString;
349         }
  1 /// <summary>
  2         /// 
  3         /// </summary>
  4         /// <param name="bytes"></param>
  5         /// <param name="iLength"></param>
  6         /// <returns></returns>
  7         public static string ToHexString(byte[] bytes, int iLength) // 0xae00cf => "AE00CF "
  8         {
  9             string hexString = string.Empty;
 10             if (bytes != null)
 11             {
 12                 StringBuilder strB = new StringBuilder();
 13 
 14                 if (bytes.Length < iLength)
 15                 {
 16                     iLength = bytes.Length;
 17                 }
 18 
 19                 for (int i = 0; i < iLength; i++)
 20                 {
 21                     strB.Append(bytes[i].ToString("X2"));
 22                 }
 23                 hexString = strB.ToString();
 24             }
 25             return hexString;
 26         }
 27 
 28         /// <summary>
 29         /// 将byte数组转换为16进制字符串
 30         /// </summary>
 31         /// <param name="bytes">要转换的数组</param>
 32         /// <param name="iStart">数组下标</param>
 33         /// <param name="iLength">长度</param>
 34         /// <returns></returns>
 35         public static string ToHexString(byte[] bytes, int iStart, int iLength) // 0xae00cf => "AE00CF "
 36         {
 37             string hexString = string.Empty;
 38             if (bytes != null)
 39             {
 40                 StringBuilder strB = new StringBuilder();
 41 
 42                 //缓冲区长度问题,需清空缓冲区
 43                 if (bytes.Length < (iLength + iStart))
 44                 {
 45                     iLength = bytes.Length;
 46                 }
 47 
 48                 for (int i = iStart; i < iLength + iStart; i++)
 49                 {
 50                     strB.Append(bytes[i].ToString("X2"));
 51                 }
 52                 hexString = strB.ToString();
 53             }
 54             return hexString;
 55         }
 56 
 57         /// <summary>
 58         /// 
 59         /// </summary>
 60         /// <param name="hexString"></param>
 61         /// <param name="discarded"></param>
 62         /// <returns></returns>
 63         public static byte[] GetBytes(string hexString, out int discarded)
 64         {
 65             discarded = 0;
 66             string newString = "";
 67             char c;
 68             // remove all none A-F, 0-9, characters
 69             for (int i = 0; i < hexString.Length; i++)
 70             {
 71                 c = hexString[i];
 72                 if (Uri.IsHexDigit(c))
 73                     newString += c;
 74                 else
 75                     discarded++;
 76             }
 77             // if odd number of characters, discard last character
 78             if (newString.Length%2 != 0)
 79             {
 80                 discarded++;
 81                 newString = newString.Substring(0, newString.Length - 1);
 82             }
 83 
 84             return HexToByte(newString);
 85         }
 86 
 87         /// <summary>
 88         /// Converts from binary coded decimal to integer
 89         /// </summary>
 90         /// <param name="num"></param>
 91         /// <returns></returns>
 92         public static uint BcdToDec(uint num)
 93         {
 94             return HornerScheme(num, 0x10, 10);
 95         }
 96 
 97         /// <summary>
 98         /// Converts from integer to binary coded decimal
 99         /// </summary>
100         /// <param name="num"></param>
101         /// <returns></returns>
102         public static uint DecToBcd(uint num)
103         {
104             return HornerScheme(num, 10, 0x10);
105         }
106 
107         private static uint HornerScheme(uint num, uint divider, uint factor)
108         {
109             uint remainder = 0, quotient = 0, result = 0;
110             remainder = num%divider;
111             quotient = num/divider;
112             if (!(quotient == 0 && remainder == 0))
113                 result += HornerScheme(quotient, divider, factor)*factor + remainder;
114             return result;
115         }
116 
117         /// <summary>
118         /// byte数组尾部0截取函数
119         /// </summary>
120         /// <param name="buf">原始byte数组</param>
121         /// <param name="iLength">要截取的长度</param>
122         /// <returns>截取后的数组</returns>
123         public static byte[] InterceptByte(byte[] buf, int iLength)
124         {
125             StringBuilder sb = new StringBuilder(iLength*2);
126             sb = sb.Append(ToHexString(buf, (short) iLength));
127             int discarded = 0;
128             byte[] bReturn = GetBytes(sb.ToString(), out discarded);
129 
130             if (discarded > 0)
131             {
132                 throw new Exception("byte数组截取有数据丢失!");
133             }
134             return bReturn;
135         }
136 
137         /// <summary>
138         /// 
139         /// </summary>
140         /// <param name="hexString"></param>
141         /// <returns></returns>
142         public static byte[] HexToByte(string hexString)
143         {
144             if (string.IsNullOrEmpty(hexString))
145             {
146                 hexString = "00";
147             }
148             byte[] returnBytes = new byte[hexString.Length/2];
149             for (int i = 0; i < returnBytes.Length; i++)
150                 returnBytes[i] = Convert.ToByte(hexString.Substring(i*2, 2), 16);
151             return returnBytes;
152         }
153 
154         /// <summary>
155         /// 日期转BCD数组
156         /// </summary>
157         /// <param name="dateTime"></param>
158         /// <param name="type">4 6 7</param>
159         /// <returns></returns>
160         public static byte[] DateTimeToBCD(DateTime dateTime, ushort type)
161         {
162             string strServerTime = string.Format("{0:yyyyMMddHHmmss}", dateTime);
163 
164             byte[] bcd = new byte[type];
165             if (type == 4)
166             {
167                 bcd[0] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(0, 2))).ToString("D2"));
168                 bcd[1] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(2, 2))).ToString("D2"));
169                 bcd[2] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(4, 2))).ToString("D2"));
170                 bcd[3] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(6, 2))).ToString("D2"));
171             }
172             if (type == 6)
173             {
174                 bcd[0] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(2, 2))).ToString("D2"));
175                 bcd[1] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(4, 2))).ToString("D2"));
176                 bcd[2] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(6, 2))).ToString("D2"));
177                 bcd[3] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(8, 2))).ToString("D2"));
178                 bcd[4] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(10, 2))).ToString("D2"));
179                 bcd[5] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(12, 2))).ToString("D2"));
180             }
181             if (type == 7)
182             {
183                 bcd[0] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(0, 2))).ToString("D2"));
184                 bcd[1] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(2, 2))).ToString("D2"));
185                 bcd[2] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(4, 2))).ToString("D2"));
186                 bcd[3] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(6, 2))).ToString("D2"));
187                 bcd[4] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(8, 2))).ToString("D2"));
188                 bcd[5] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(10, 2))).ToString("D2"));
189                 bcd[5] = byte.Parse(DecToBcd(uint.Parse(strServerTime.Substring(12, 2))).ToString("D2"));
190             }
191             return bcd;
192         }
193 
194         /// <summary>
195         /// BCD时间转日期时间
196         /// </summary>
197         /// <param name="bcdTime"></param>
198         /// <param name="type"></param>
199         /// <returns></returns>
200         public static DateTime BCDToDateTime(byte[] bcdTime, ushort type)
201         {
202             StringBuilder sb = new StringBuilder();
203             if (type == 4) //4位BCD码的日期
204             {
205                 sb.Append(BcdToDec(bcdTime[0]).ToString("D2"));
206                 sb.Append(BcdToDec(bcdTime[1]).ToString("D2"));
207                 sb.Append('-' + BcdToDec(bcdTime[2]).ToString("D2"));
208                 sb.Append('-' + BcdToDec(bcdTime[3]).ToString("D2") + " ");
209             }
210             if (type == 6) //6位BCD码的时间
211             {
212                 sb.Append(DateTime.Now.ToString("yyyy").Substring(0, 2));
213                 sb.Append(BcdToDec(bcdTime[0]).ToString("D2"));
214                 sb.Append('-' + BcdToDec(bcdTime[1]).ToString("D2"));
215                 sb.Append('-' + BcdToDec(bcdTime[2]).ToString("D2") + " ");
216                 sb.Append(BcdToDec(bcdTime[3]).ToString("D2") + ":");
217                 sb.Append(BcdToDec(bcdTime[4]).ToString("D2") + ":");
218                 sb.Append(BcdToDec(bcdTime[5]));
219             }
220             if (type == 7) //7位BCD码的日期
221             {
222                 sb.Append(BcdToDec(bcdTime[0]).ToString("D2"));
223                 sb.Append(BcdToDec(bcdTime[1]).ToString("D2"));
224                 sb.Append('-' + BcdToDec(bcdTime[2]).ToString("D2"));
225                 sb.Append('-' + BcdToDec(bcdTime[3]).ToString("D2") + " ");
226                 sb.Append(BcdToDec(bcdTime[4]).ToString("D2") + ":");
227                 sb.Append(BcdToDec(bcdTime[5]).ToString("D2") + ":");
228                 sb.Append(BcdToDec(bcdTime[6]));
229             }
230 
231             DateTime dt;
232             //2011-3-26 当日期出错时的处理
233             DateTime.TryParse(sb.ToString(), out dt);
234 
235             return dt;
236         }
237     }
238 }

 

http://www.cnblogs.com/jhabb/archive/2011/05/06/2038777.html[落冰]

posted @ 2012-10-24 11:27  GeneralKING  阅读(18931)  评论(2编辑  收藏  举报