using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; namespace OrpaonEMS.App.Com { /// /// 处理数据类型转换,数制转换、编码转换相关的类 /// public sealed class ConvertHelper { #region 补足位数 /// /// 指定字符串的固定长度,如果字符串小于固定长度, /// 则在字符串的前面补足零,可设置的固定长度最大为9位 /// /// 原始字符串 /// 字符串的固定长度 public static string RepairZero(string text, int limitedLength) { //补足0的字符串 string temp = ""; //补足0 for (int i = 0; i < limitedLength - text.Length; i++) { temp += "0"; } //连接text temp += text; //返回补足0的字符串 return temp; } #endregion #region 各进制数间转换 /// /// 实现各进制数间的转换。 /// /// 要转换的值,即原值 /// 原值的进制,只能是2,8,10,16四个值。 /// 要转换到的目标进制,只能是2,8,10,16四个值。 /// 转换失败返回“0” /// /// ConvertBase("15",10,16); 表示将十进制数15转换为16进制的数。 /// public static string ConvertBase(string value, int from, int to) { try { int intValue = Convert.ToInt32(value, from); //先转成10进制 string result = Convert.ToString(intValue, to); //再转成目标进制 if (to == 2) { result = result.PadLeft(8, '0'); } return result; } catch (Exception ex) { Console.WriteLine(ex); return "0"; } } #endregion #region 使用指定字符集将string转换成byte[] /// /// 使用指定字符集将string转换成byte[] /// /// 要转换的字符串 /// 字符编码 public static byte[] StringToBytes(string text, Encoding encoding) { return encoding.GetBytes(text); } #endregion #region byte[]转string /// /// 使用指定字符集将byte[]转换成string /// /// 要转换的字节数组 /// 字符编码 public static string BytesToString(byte[] bytes, Encoding encoding) { return encoding.GetString(bytes); } /// /// 从字节数组生成字符串; /// /// /// public string GetBufferStr(byte[] buffer) { //byte SEP = 0xB3 /*分隔符|*/; string result = ""; for (int i = 0; i < buffer.Length; i++) { switch (buffer[i]) { //case SEP: // result += "|"; // break; default: string str = (char)buffer[i] + ""; if (!string.IsNullOrEmpty(str)) { result += str; } break; } } return result; } #endregion #region 将byte[]转换成int /// /// 将byte[]转换成int /// /// 需要转换成整数的byte数组 public static int BytesToInt32(byte[] data) { //如果传入的字节数组长度小于4,则返回0 if (data.Length < 4) { return 0; } //定义要返回的整数 int num = 0; //如果传入的字节数组长度大于4,需要进行处理 if (data.Length >= 4) { //创建一个临时缓冲区 byte[] tempBuffer = new byte[4]; //将传入的字节数组的前4个字节复制到临时缓冲区 Buffer.BlockCopy(data, 0, tempBuffer, 0, 4); //将临时缓冲区的值转换成整数,并赋给num num = BitConverter.ToInt32(tempBuffer, 0); } //返回整数 return num; } #endregion #region struct和byte[]互转 public static byte[] StructToBytes(object structObj) { int size = Marshal.SizeOf(structObj); IntPtr buffer = Marshal.AllocHGlobal(size); try { Marshal.StructureToPtr(structObj, buffer, false); byte[] bytes = new byte[size]; Marshal.Copy(buffer, bytes, 0, size); return bytes; } finally { Marshal.FreeHGlobal(buffer); } } public static T BytesToStruct(byte[] data) { IntPtr buffer = Marshal.AllocHGlobal(data.Length); try { Marshal.Copy(data, 0, buffer, data.Length); return Marshal.PtrToStructure(buffer); } finally { Marshal.FreeHGlobal(buffer); } } #endregion /// /// 通用类型转换 /// http://www.dzwebs.net/4343.html /// /// 值 /// 类型 /// object public static object ChangeType(object value, Type type) { if (value == null && type.IsGenericType) return Activator.CreateInstance(type); if (value == null) return null; if (type == value.GetType()) return value; if (type.IsEnum) { if (value is string) return Enum.Parse(type, value as string); else return Enum.ToObject(type, value); } if (!type.IsInterface && type.IsGenericType) { Type innerType = type.GetGenericArguments()[0]; object innerValue = ChangeType(value, innerType); return Activator.CreateInstance(type, new object[] { innerValue }); } if (value is string && type == typeof(Guid)) return new Guid(value as string); if (value is string && type == typeof(Version)) return new Version(value as string); //自定义类转换会在这句返回; if (!(value is IConvertible)) return value; return Convert.ChangeType(value, type); } } }