using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace EQUIPINTERFACETEST { class ConvertClass { /// /// 十进制转换为二进制 /// /// /// public static string DecToBin(string x) { string z = null; int X = Convert.ToInt32(x); int i = 0; long a, b = 0; while (X > 0) { a = X%2; X = X/2; b = b + a*Pow(10, i); i++; } z = Convert.ToString(b); return z; } /// /// 16进制转ASCII码 /// /// /// public static string HexToAscii(string hexString) { StringBuilder sb = new StringBuilder(); for (int i = 0; i <= hexString.Length - 2; i += 2) { sb.Append( Convert.ToString( Convert.ToChar(Int32.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber)))); } return sb.ToString(); } public static string DecToAscii(string hexString) { StringBuilder sb = new StringBuilder(); for (int i = 0; i <= hexString.Length - 2; i += 2) { sb.Append( Convert.ToString( Convert.ToChar(Int32.Parse(hexString.Substring(i, 2), System.Globalization.NumberStyles.HexNumber)))); } return sb.ToString(); } /// /// 十进制转换为八进制 /// /// /// public static string DecToOtc(string x) { string z = null; int X = Convert.ToInt32(x); int i = 0; long a, b = 0; while (X > 0) { a = X%8; X = X/8; b = b + a*Pow(10, i); i++; } z = Convert.ToString(b); return z; } /// /// 十进制转换为十六进制 /// /// /// public static string DecToHex(string x) { if (string.IsNullOrEmpty(x)) { return "0"; } string z = null; int X = Convert.ToInt32(x); Stack a = new Stack(); int i = 0; while (X > 0) { a.Push(Convert.ToString(X%16)); X = X/16; i++; } while (a.Count != 0) z += ToHex(Convert.ToString(a.Pop())); if (string.IsNullOrEmpty(z)) { z = "0"; } return z; } /// /// 二进制转换为十进制 /// /// /// public static string BinToDec(string x) { string z = null; int X = Convert.ToInt32(x); int i = 0; long a, b = 0; while (X > 0) { a = X%10; X = X/10; b = b + a*Pow(2, i); i++; } z = Convert.ToString(b); return z; } /// /// 二进制转换为十进制,定长转换 /// /// /// /// public static string BinToDec(string x, short iLength) { StringBuilder sb = new StringBuilder(); int iCount = 0; iCount = x.Length/iLength; if (x.Length%iLength > 0) { iCount += 1; } int X = 0; for (int i = 0; i < iCount; i++) { if ((i + 1)*iLength > x.Length) { X = Convert.ToInt32(x.Substring(i*iLength, (x.Length - iLength))); } else { X = Convert.ToInt32(x.Substring(i*iLength, iLength)); } int j = 0; long a, b = 0; while (X > 0) { a = X%10; X = X/10; b = b + a*Pow(2, j); j++; } sb.AppendFormat("{0:D2}", b); } return sb.ToString(); } /// /// 二进制转换为十六进制,定长转换 /// /// /// /// public static string BinToHex(string x, short iLength) { StringBuilder sb = new StringBuilder(); int iCount = 0; iCount = x.Length/iLength; if (x.Length%iLength > 0) { iCount += 1; } int X = 0; for (int i = 0; i < iCount; i++) { if ((i + 1)*iLength > x.Length) { X = Convert.ToInt32(x.Substring(i*iLength, (x.Length - iLength))); } else { X = Convert.ToInt32(x.Substring(i*iLength, iLength)); } int j = 0; long a, b = 0; while (X > 0) { a = X%10; X = X/10; b = b + a*Pow(2, j); j++; } //前补0 sb.Append(DecToHex(b.ToString())); } return sb.ToString(); } /// /// 八进制转换为十进制 /// /// /// public static string OctToDec(string x) { string z = null; int X = Convert.ToInt32(x); int i = 0; long a, b = 0; while (X > 0) { a = X%10; X = X/10; b = b + a*Pow(8, i); i++; } z = Convert.ToString(b); return z; } /// /// 十六进制转换为十进制 /// /// /// public static string HexToDec(string x) { if (string.IsNullOrEmpty(x)) { return "0"; } string z = null; Stack a = new Stack(); int i = 0, j = 0, l = x.Length; long Tong = 0; while (i < l) { a.Push(ToDec(Convert.ToString(x[i]))); i++; } while (a.Count != 0) { Tong = Tong + Convert.ToInt64(a.Pop())*Pow(16, j); j++; } z = Convert.ToString(Tong); return z; } /// /// /// /// /// /// private static long Pow(long x, long y) { int i = 1; long X = x; if (y == 0) return 1; while (i < y) { x = x*X; i++; } return x; } /// /// /// /// /// private static string ToDec(string x) { switch (x) { case "A": return "10"; case "B": return "11"; case "C": return "12"; case "D": return "13"; case "E": return "14"; case "F": return "15"; default: return x; } } /// /// /// /// /// private static string ToHex(string x) { switch (x) { case "10": return "A"; case "11": return "B"; case "12": return "C"; case "13": return "D"; case "14": return "E"; case "15": return "F"; default: return x; } } /// /// 将16进制BYTE数组转换成16进制字符串 /// /// /// public static string ToHexString(byte[] bytes) // 0xae00cf => "AE00CF " { string hexString = string.Empty; if (bytes != null) { StringBuilder strB = new StringBuilder(); for (int i = 0; i < bytes.Length; i++) { strB.Append(bytes[i].ToString("X2")); } hexString = strB.ToString(); } return hexString; } public static int ToBCD(byte val) { int res = 0; int bit = 0; while (val >= 10) { res |= (val % 10 << bit); val /= 10; bit += 4; } res |= val << bit; return res; } public static byte FromBCD(int vals) { int c = 1; byte b = 0; while (vals > 0) { b += (byte)((vals & 0xf) * c); c *= 10; vals >>= 4; } return b; } public static int ConvertS5TimeToInt32(int init) { int qianwei, baiwei, shiwei, gewei; int beishu = 0; int result = 0; qianwei = init / 4096; init = init % 4096; baiwei = init / 256; init = init % 256; shiwei = init / 16; gewei = init % 16; switch (qianwei) { case 0: beishu = 10; break; case 1: beishu = 100; break; case 2: beishu = 1000; break; case 3: beishu = 10000; break; } result = (baiwei * 100 + shiwei * 10 + gewei) * beishu; return result; } public static int ConvertInt32ToS5Time(int init) { int qianwei, baiwei, shiwei, gewei; int result = 0; if (0 <= init && init <= 4095) { init = init / 10; qianwei = 0; baiwei = init / 100; init = init % 100; shiwei = init / 10; gewei = init % 10; result = qianwei * 4096 + baiwei * 256 + shiwei * 16 + gewei; } else if (0 <= init / 10 && init / 10 <= 4095) { init = init / 100; qianwei = 1; baiwei = init / 100; init = init % 100; shiwei = init / 10; gewei = init % 10; result = qianwei * 4096 + baiwei * 256 + shiwei * 16 + gewei; } else if (0 <= init / 100 && init / 100 <= 4095) { init = init / 1000; qianwei = 2; baiwei = init / 100; init = init % 100; shiwei = init / 10; gewei = init % 10; result = qianwei * 4096 + baiwei * 256 + shiwei * 16 + gewei; } else if (0 <= init / 1000 && init / 1000 <= 4095) { init = init / 10000; qianwei = 3; baiwei = init / 100; init = init % 100; shiwei = init / 10; gewei = init % 10; result = qianwei * 4096 + baiwei * 256 + shiwei * 16 + gewei; } return result; } } }