using System; using System.Collections.Generic; using System.Linq; using System.Text; #if !NETSTANDARD2_0 using System.Windows.Forms; #endif using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Security.Cryptography; using System.Drawing; namespace MECF.Framework.RT.Core.IoProviders.Siemens.Transfer { /// /// 一个软件基础类,提供常用的一些静态方法 -> /// A software-based class that provides some common static methods /// public class SoftBasic { #region MD5 Calculate /// /// 获取文件的md5码 -> Get the MD5 code of the file /// /// 文件的路径,既可以是完整的路径,也可以是相对的路径 -> The path to the file /// Md5字符串 /// /// 下面举例实现获取一个文件的md5码 /// /// public static string CalculateFileMD5( string filePath ) { string str_md5 = string.Empty; using (FileStream fs = new FileStream( filePath, FileMode.Open, FileAccess.Read )) { str_md5 = CalculateStreamMD5( fs ); } return str_md5; } /// /// 获取数据流的md5码 -> Get the MD5 code for the data stream /// /// 数据流,可以是内存流,也可以是文件流 /// Md5字符串 /// /// 下面举例实现获取一个流的md5码 /// /// public static string CalculateStreamMD5( Stream stream ) { byte[] bytes_md5 = null; using (MD5 md5 = new MD5CryptoServiceProvider( )) { bytes_md5 = md5.ComputeHash( stream ); } return BitConverter.ToString( bytes_md5 ).Replace( "-", "" ); } /// /// 获取文本字符串信息的Md5码,编码为UTF8 /// /// 文本数据信息 /// Md5字符串 public static string CalculateStreamMD5( string data ) { return CalculateStreamMD5( data, Encoding.UTF8 ); } /// /// 获取文本字符串信息的Md5码,使用指定的编码 /// /// 文本数据信息 /// 编码信息 /// Md5字符串 public static string CalculateStreamMD5( string data, Encoding encode ) { string str_md5 = string.Empty; using (MD5 md5 = new MD5CryptoServiceProvider( )) { byte[] bytes_md5 = md5.ComputeHash( encode.GetBytes( data ) ); str_md5 = BitConverter.ToString( bytes_md5 ).Replace( "-", "" ); } return str_md5; } #if !NETSTANDARD2_0 /// /// 获取内存图片的md5码 -> Get the MD5 code of the memory picture /// /// 内存图片 /// Md5字符串 /// /// 下面举例实现获取一个图像的md5码 /// /// public static string CalculateStreamMD5(Bitmap bitmap) { MemoryStream ms = new MemoryStream(); bitmap.Save(ms, bitmap.RawFormat); MD5 md5 = new MD5CryptoServiceProvider(); byte[] bytes_md5 = md5.ComputeHash(ms); ms.Dispose(); return BitConverter.ToString(bytes_md5).Replace("-", ""); } #endif #endregion #region DataSize Format /// /// 从一个字节大小返回带单位的描述 /// /// 实际的大小值 /// 最终的字符串值 /// /// 比如说我们获取了文件的长度,这个长度可以来自于本地,也可以来自于数据库查询 /// /// public static string GetSizeDescription( long size ) { if (size < 1000) { return size + " B"; } else if (size < 1000 * 1000) { float data = (float)size / 1024; return data.ToString( "F2" ) + " Kb"; } else if (size < 1000 * 1000 * 1000) { float data = (float)size / 1024 / 1024; return data.ToString( "F2" ) + " Mb"; } else { float data = (float)size / 1024 / 1024 / 1024; return data.ToString( "F2" ) + " Gb"; } } #endregion #region TimeSpan Format /// /// 从一个时间差返回带单位的描述 /// /// 实际的时间差 /// 最终的字符串值 /// /// 比如说我们获取了一个时间差信息 /// /// public static string GetTimeSpanDescription( TimeSpan ts ) { if (ts.TotalSeconds <= 60) { return (int)ts.TotalSeconds + " 秒"; } else if (ts.TotalMinutes <= 60) { return ts.TotalMinutes.ToString( "F1" ) + " 分钟"; } else if (ts.TotalHours <= 24) { return ts.TotalHours.ToString( "F1" ) + " 小时"; } else { return ts.TotalDays.ToString( "F1" ) + " 天"; } } #endregion #region Array Expaned /// /// 一个通用的数组新增个数方法,会自动判断越界情况,越界的情况下,会自动的截断或是填充 -> /// A common array of new methods, will automatically determine the cross-border situation, in the case of cross-border, will be automatically truncated or filled /// /// 数据类型 /// 原数据 /// 等待新增的数据 /// 原数据的最大值 /// /// /// public static void AddArrayData( ref T[] array, T[] data, int max ) { if (data == null) return; // 数据为空 if (data.Length == 0) return; // 数据长度为空 if (array.Length == max) { Array.Copy( array, data.Length, array, 0, array.Length - data.Length ); Array.Copy( data, 0, array, array.Length - data.Length, data.Length ); } else { if ((array.Length + data.Length) > max) { T[] tmp = new T[max]; for (int i = 0; i < (max - data.Length); i++) { tmp[i] = array[i + (array.Length - max + data.Length)]; } for (int i = 0; i < data.Length; i++) { tmp[tmp.Length - data.Length + i] = data[i]; } // 更新数据 array = tmp; } else { T[] tmp = new T[array.Length + data.Length]; for (int i = 0; i < array.Length; i++) { tmp[i] = array[i]; } for (int i = 0; i < data.Length; i++) { tmp[tmp.Length - data.Length + i] = data[i]; } array = tmp; } } } /// /// 将一个数组进行扩充到指定长度,或是缩短到指定长度 -> /// Extend an array to a specified length, or shorten to a specified length or fill /// /// 数组的类型 /// 原先数据的数据 /// 新数组的长度 /// 新数组长度信息 /// /// /// public static T[] ArrayExpandToLength( T[] data, int length ) { if (data == null) return new T[length]; if (data.Length == length) return data; T[] buffer = new T[length]; Array.Copy( data, buffer, Math.Min( data.Length, buffer.Length ) ); return buffer; } /// /// 将一个数组进行扩充到偶数长度 -> /// Extend an array to even lengths /// /// 数组的类型 /// 原先数据的数据 /// 新数组长度信息 /// /// /// public static T[] ArrayExpandToLengthEven( T[] data ) { if (data == null) return new T[0]; if (data.Length % 2 == 1) { return ArrayExpandToLength( data, data.Length + 1 ); } else { return data; } } #endregion #region Byte Array compare /// /// 判断两个字节的指定部分是否相同 -> /// Determines whether the specified portion of a two-byte is the same /// /// 第一个字节 /// 第一个字节的起始位置 /// 第二个字节 /// 第二个字节的起始位置 /// 校验的长度 /// 返回是否相等 /// /// /// /// public static bool IsTwoBytesEquel( byte[] b1, int start1, byte[] b2, int start2, int length ) { if (b1 == null || b2 == null) return false; for (int i = 0; i < length; i++) { if (b1[i + start1] != b2[i + start2]) { return false; } } return true; } /// /// 判断两个字节的指定部分是否相同 -> /// Determines whether the specified portion of a two-byte is the same /// /// 第一个字节 /// 第二个字节 /// 返回是否相等 /// /// /// public static bool IsTwoBytesEquel( byte[] b1, byte[] b2) { if (b1 == null || b2 == null) return false; if (b1.Length != b2.Length) return false; return IsTwoBytesEquel( b1, 0, b2, 0, b1.Length ); } /// /// 判断两个数据的令牌是否相等 -> /// Determines whether the tokens of two data are equal /// /// 字节数据 /// GUID数据 /// 返回是否相等 /// /// /// public static bool IsByteTokenEquel( byte[] head, Guid token ) { return IsTwoBytesEquel( head, 12, token.ToByteArray( ), 0, 16 ); } /// /// 判断两个数据的令牌是否相等 -> /// Determines whether the tokens of two data are equal /// /// 第一个令牌 /// 第二个令牌 /// 返回是否相等 public static bool IsTwoTokenEquel( Guid token1, Guid token2 ) { return IsTwoBytesEquel( token1.ToByteArray( ), 0, token2.ToByteArray( ), 0, 16 ); } #endregion #region Enum About /// /// 获取一个枚举类型的所有枚举值,可直接应用于组合框数据 -> /// Gets all the enumeration values of an enumeration type that can be applied directly to the combo box data /// /// 枚举的类型值 /// 枚举值数组 /// /// /// public static TEnum[] GetEnumValues( ) where TEnum : struct { return (TEnum[])Enum.GetValues( typeof( TEnum ) ); } /// /// 从字符串的枚举值数据转换成真实的枚举值数据 -> /// Convert enumeration value data from strings to real enumeration value data /// /// 枚举的类型值 /// 枚举的字符串的数据值 /// 真实的枚举值 /// /// /// public static TEnum GetEnumFromString(string value ) where TEnum : struct { return (TEnum)Enum.Parse( typeof( TEnum ), value ); } #endregion #region Exception Message Format #if !NETSTANDARD2_0 /// /// 显示一个完整的错误信息 -> /// Displays a complete error message /// /// 异常对象 /// 调用本方法可以显示一个异常的详细信息 /// public static void ShowExceptionMessage( Exception ex ) { MessageBox.Show( GetExceptionMessage( ex ) ); } /// /// 显示一个完整的错误信息,和额外的字符串描述信息 -> /// Displays a complete error message, and additional string description information /// /// 额外的描述信息 /// 调用本方法可以显示一个异常的详细信息 /// 异常对象 /// public static void ShowExceptionMessage( string extraMsg, Exception ex ) { MessageBox.Show( GetExceptionMessage( extraMsg, ex ) ); } #endif /// /// 获取一个异常的完整错误信息 -> /// Gets the complete error message for an exception /// /// 异常对象 /// 完整的字符串数据 /// 获取异常的完整信息 /// ex不能为空 /// /// /// public static string GetExceptionMessage( Exception ex ) { return ex.Message + Environment.NewLine + ex.StackTrace + Environment.NewLine + ex.TargetSite; } /// /// 获取一个异常的完整错误信息,和额外的字符串描述信息 -> /// Gets the complete error message for an exception, and additional string description information /// /// 额外的信息 /// 异常对象 /// 完整的字符串数据 /// /// /// /// public static string GetExceptionMessage( string extraMsg, Exception ex ) { if (string.IsNullOrEmpty( extraMsg )) { return GetExceptionMessage( ex ); } else { return extraMsg + Environment.NewLine + GetExceptionMessage( ex ); } } #endregion #region Hex string and Byte[] transform /// /// 字节数据转化成16进制表示的字符串 -> /// Byte data into a string of 16 binary representations /// /// 字节数组 /// 返回的字符串 /// /// /// /// public static string ByteToHexString( byte[] InBytes ) { return ByteToHexString( InBytes, (char)0 ); } /// /// 字节数据转化成16进制表示的字符串 -> /// Byte data into a string of 16 binary representations /// /// 字节数组 /// 分割符 /// 返回的字符串 /// /// /// /// public static string ByteToHexString( byte[] InBytes, char segment ) { StringBuilder sb = new StringBuilder( ); foreach (byte InByte in InBytes) { if (segment == 0) sb.Append( string.Format( "{0:X2}", InByte ) ); else sb.Append( string.Format( "{0:X2}{1}", InByte, segment ) ); } if (segment != 0 && sb.Length > 1 && sb[sb.Length - 1] == segment) { sb.Remove( sb.Length - 1, 1 ); } return sb.ToString( ); } /// /// 字符串数据转化成16进制表示的字符串 -> /// String data into a string of 16 binary representations /// /// 输入的字符串数据 /// 返回的字符串 /// public static string ByteToHexString( string InString ) { return ByteToHexString( Encoding.Unicode.GetBytes( InString ) ); } private static List hexCharList = new List( ) { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F' }; /// /// 将16进制的字符串转化成Byte数据,将检测每2个字符转化,也就是说,中间可以是任意字符 -> /// Converts a 16-character string into byte data, which will detect every 2 characters converted, that is, the middle can be any character /// /// 十六进制的字符串,中间可以是任意的分隔符 /// 转换后的字节数组 /// 参数举例:AA 01 34 A8 /// /// /// public static byte[] HexStringToBytes( string hex ) { hex = hex.ToUpper( ); MemoryStream ms = new MemoryStream( ); for (int i = 0; i < hex.Length; i++) { if ((i + 1) < hex.Length) { if (hexCharList.Contains( hex[i] ) && hexCharList.Contains( hex[i + 1] )) { // 这是一个合格的字节数据 ms.WriteByte( (byte)(hexCharList.IndexOf( hex[i] ) * 16 + hexCharList.IndexOf( hex[i + 1] )) ); i++; } } } byte[] result = ms.ToArray( ); ms.Dispose( ); return result; } #endregion #region Byte Reverse By Word /// /// 将byte数组按照双字节进行反转,如果为单数的情况,则自动补齐 -> /// Reverses the byte array by double byte, or if the singular is the case, automatically /// /// 输入的字节信息 /// 反转后的数据 /// /// /// public static byte[] BytesReverseByWord(byte[] inBytes ) { if (inBytes == null) return null; byte[] buffer = ArrayExpandToLengthEven( inBytes ); for (int i = 0; i < buffer.Length / 2; i++) { byte tmp = buffer[i * 2 + 0]; buffer[i * 2 + 0] = buffer[i * 2 + 1]; buffer[i * 2 + 1] = tmp; } return buffer; } #endregion #region Byte[] and AsciiByte[] transform /// /// 将原始的byte数组转换成ascii格式的byte数组 -> /// Converts the original byte array to an ASCII-formatted byte array /// /// 等待转换的byte数组 /// 转换后的数组 public static byte[] BytesToAsciiBytes( byte[] inBytes ) { return Encoding.ASCII.GetBytes( ByteToHexString( inBytes ) ); } /// /// 将ascii格式的byte数组转换成原始的byte数组 -> /// Converts an ASCII-formatted byte array to the original byte array /// /// 等待转换的byte数组 /// 转换后的数组 public static byte[] AsciiBytesToBytes( byte[] inBytes ) { return HexStringToBytes( Encoding.ASCII.GetString( inBytes ) ); } /// /// 从字节构建一个ASCII格式的数据内容 /// /// 数据 /// ASCII格式的字节数组 public static byte[] BuildAsciiBytesFrom( byte value ) { return Encoding.ASCII.GetBytes( value.ToString( "X2" ) ); } /// /// 从short构建一个ASCII格式的数据内容 /// /// 数据 /// ASCII格式的字节数组 public static byte[] BuildAsciiBytesFrom( short value ) { return Encoding.ASCII.GetBytes( value.ToString( "X4" ) ); } /// /// 从ushort构建一个ASCII格式的数据内容 /// /// 数据 /// ASCII格式的字节数组 public static byte[] BuildAsciiBytesFrom( ushort value ) { return Encoding.ASCII.GetBytes( value.ToString( "X4" ) ); } #endregion #region Bool[] and byte[] transform /// /// 将bool数组转换到byte数组 -> /// Converting a bool array to a byte array /// /// bool数组 /// 转换后的字节数组 /// /// /// public static byte[] BoolArrayToByte( bool[] array ) { if (array == null) return null; int length = array.Length % 8 == 0 ? array.Length / 8 : array.Length / 8 + 1; byte[] buffer = new byte[length]; for (int i = 0; i < array.Length; i++) { int index = i / 8; int offect = i % 8; byte temp = 0; switch (offect) { case 0: temp = 0x01; break; case 1: temp = 0x02; break; case 2: temp = 0x04; break; case 3: temp = 0x08; break; case 4: temp = 0x10; break; case 5: temp = 0x20; break; case 6: temp = 0x40; break; case 7: temp = 0x80; break; default: break; } if (array[i]) buffer[index] += temp; } return buffer; } /// /// 从Byte数组中提取位数组,length代表位数 -> /// Extracts a bit array from a byte array, length represents the number of digits /// /// 原先的字节数组 /// 想要转换的长度,如果超出自动会缩小到数组最大长度 /// 转换后的bool数组 /// /// /// public static bool[] ByteToBoolArray( byte[] InBytes, int length ) { if (InBytes == null) return null; if (length > InBytes.Length * 8) length = InBytes.Length * 8; bool[] buffer = new bool[length]; for (int i = 0; i < length; i++) { int index = i / 8; int offect = i % 8; byte temp = 0; switch (offect) { case 0: temp = 0x01; break; case 1: temp = 0x02; break; case 2: temp = 0x04; break; case 3: temp = 0x08; break; case 4: temp = 0x10; break; case 5: temp = 0x20; break; case 6: temp = 0x40; break; case 7: temp = 0x80; break; default: break; } if ((InBytes[index] & temp) == temp) { buffer[i] = true; } } return buffer; } /// /// 从Byte数组中提取所有的位数组 -> /// Extracts a bit array from a byte array, length represents the number of digits /// /// 原先的字节数组 /// 转换后的bool数组 /// /// /// public static bool[] ByteToBoolArray( byte[] InBytes ) { if (InBytes == null) return null; return ByteToBoolArray( InBytes, InBytes.Length * 8 ); } #endregion #region Byte[] Splice /// /// 拼接2个字节数组成一个数组 -> /// Splicing 2 bytes to to an array /// /// 数组一 /// 数组二 /// 拼接后的数组 /// /// /// public static byte[] SpliceTwoByteArray( byte[] bytes1, byte[] bytes2 ) { if (bytes1 == null && bytes2 == null) return null; if (bytes1 == null) return bytes2; if (bytes2 == null) return bytes1; byte[] buffer = new byte[bytes1.Length + bytes2.Length]; bytes1.CopyTo( buffer, 0 ); bytes2.CopyTo( buffer, bytes1.Length ); return buffer; } /// /// 将一个byte数组的前面指定位数移除,返回新的一个数组 -> /// Removes the preceding specified number of bits in a byte array, returning a new array /// /// 字节数组 /// 等待移除的长度 /// 新的数据 /// /// /// public static byte[] BytesArrayRemoveBegin( byte[] value, int length ) { return BytesArrayRemoveDouble( value, length, 0 ); } /// /// 将一个byte数组的后面指定位数移除,返回新的一个数组 -> /// Removes the specified number of digits after a byte array, returning a new array /// /// 字节数组 /// 等待移除的长度 /// 新的数据 /// /// /// public static byte[] BytesArrayRemoveLast( byte[] value, int length ) { return BytesArrayRemoveDouble( value, 0, length ); } /// /// 将一个byte数组的前后移除指定位数,返回新的一个数组 -> /// Removes a byte array before and after the specified number of bits, returning a new array /// /// 字节数组 /// 前面的位数 /// 后面的位数 /// 新的数据 /// /// /// public static byte[] BytesArrayRemoveDouble( byte[] value, int leftLength, int rightLength ) { if (value == null) return null; if (value.Length <= (leftLength + rightLength)) return new byte[0]; byte[] buffer = new byte[value.Length - leftLength - rightLength]; Array.Copy( value, leftLength, buffer, 0, buffer.Length ); return buffer; } #endregion #region Deep Clone /// /// 使用序列化反序列化深度克隆一个对象,该对象需要支持序列化特性 -> /// Cloning an object with serialization deserialization depth that requires support for serialization attributes /// /// 源对象,支持序列化 /// 新的一个实例化的对象 /// /// /// /// /// 参数必须实现序列化的特性 /// /// /// /// /// public static object DeepClone( object oringinal ) { using (System.IO.MemoryStream stream = new System.IO.MemoryStream( )) { BinaryFormatter formatter = new BinaryFormatter( ) { Context = new System.Runtime.Serialization.StreamingContext( System.Runtime.Serialization.StreamingContextStates.Clone ) }; formatter.Serialize( stream, oringinal ); stream.Position = 0; return formatter.Deserialize( stream ); } } #endregion #region Unique String Get /// /// 获取一串唯一的随机字符串,长度为20,由Guid码和4位数的随机数组成,保证字符串的唯一性 -> /// Gets a string of unique random strings with a length of 20, consisting of a GUID code and a 4-digit random number to guarantee the uniqueness of the string /// /// 随机字符串数据 /// /// /// public static string GetUniqueStringByGuidAndRandom( ) { Random random = new Random( ); return Guid.NewGuid( ).ToString( "N" ) + random.Next( 1000, 10000 ); } #endregion } }