using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Security.Cryptography; using System.Text; using System.Text.RegularExpressions; namespace SWRIS.Extensions { /// /// 一个软件基础类,提供常用的一些静态方法,比如字符串转换,字节转换的方法
/// A software-based class that provides some common static methods,Such as string conversion, byte conversion method ///
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
/// Get the Md5 code of the text string information, using the utf-8 encoding ///
/// 文本数据信息 /// Md5字符串 public static string CalculateStreamMD5(string data) => CalculateStreamMD5(data, Encoding.UTF8); /// /// 获取文本字符串信息的Md5码,使用指定的编码
/// Get the Md5 code of the text string information, using the specified encoding ///
/// 文本数据信息 /// 编码信息 /// 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 NET35 || NET451 /// /// 获取内存图片的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 ); byte[] bytes_md5 = null; using (MD5 md5 = new MD5CryptoServiceProvider( )) bytes_md5 = md5.ComputeHash( ms ); ms.Dispose( ); return ByteToHexString( bytes_md5 ); } #endif #endregion #region DataSize Format /// /// 从一个字节大小返回带单位的描述,主要是用于显示操作
/// Returns a description with units from a byte size, mainly for display operations ///
/// 实际的大小值 /// 最终的字符串值 /// /// 比如说我们获取了文件的长度,这个长度可以来自于本地,也可以来自于数据库查询 /// /// 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 Array Format /// /// 将数组格式化为显示的字符串的信息,支持所有的类型对象
/// Formats the array into the displayed string information, supporting all types of objects ///
/// 数组的类型 /// 数组信息 /// 最终显示的信息 public static string ArrayFormat(T[] array) => ArrayFormat(array, string.Empty); /// /// 将数组格式化为显示的字符串的信息,支持所有的类型对象
/// Formats the array into the displayed string information, supporting all types of objects ///
/// 数组的类型 /// 数组信息 /// 格式化的信息 /// 最终显示的信息 public static string ArrayFormat(T[] array, string format) { if (array == null) return "NULL"; StringBuilder sb = new StringBuilder("["); for (int i = 0; i < array.Length; i++) { sb.Append(string.IsNullOrEmpty(format) ? array[i].ToString() : string.Format(format, array[i])); if (i != array.Length - 1) sb.Append(","); } sb.Append("]"); return sb.ToString(); } /// /// 将数组格式化为显示的字符串的信息,支持所有的类型对象
/// Formats the array into the displayed string information, supporting all types of objects ///
/// 数组的类型 /// 数组信息 /// 最终显示的信息 public static string ArrayFormat(T array) => ArrayFormat(array, string.Empty); /// /// 将数组格式化为显示的字符串的信息,支持所有的类型对象
/// Formats the array into the displayed string information, supporting all types of objects ///
/// 数组的类型 /// 数组信息 /// 格式化的信息 /// 最终显示的信息 public static string ArrayFormat(T array, string format) { StringBuilder sb = new StringBuilder("["); if (array is Array array1) { foreach (var item in array1) { sb.Append(string.IsNullOrEmpty(format) ? item.ToString() : string.Format(format, item)); sb.Append(","); } if (array1.Length > 0 && sb[sb.Length - 1] == ',') sb.Remove(sb.Length - 1, 1); } else { sb.Append(string.IsNullOrEmpty(format) ? array.ToString() : string.Format(format, array)); } sb.Append("]"); return sb.ToString(); } #endregion #region Array Expand /// /// 一个通用的数组新增个数方法,会自动判断越界情况,越界的情况下,会自动的截断或是填充
/// 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; } /// /// 将指定的数据按照指定长度进行分割,例如int[10],指定长度4,就分割成int[4],int[4],int[2],然后拼接list
/// Divide the specified data according to the specified length, such as int [10], and specify the length of 4 to divide into int [4], int [4], int [2], and then concatenate the list ///
/// 数组的类型 /// 等待分割的数组 /// 指定的长度信息 /// 分割后结果内容 /// /// /// public static List ArraySplitByLength(T[] array, int length) { if (array == null) return new List(); List result = new List(); int index = 0; while (index < array.Length) { if (index + length < array.Length) { T[] tmp = new T[length]; Array.Copy(array, index, tmp, 0, length); index += length; result.Add(tmp); } else { T[] tmp = new T[array.Length - index]; Array.Copy(array, index, tmp, 0, tmp.Length); index += length; result.Add(tmp); } } return result; } /// /// 将整数进行有效的拆分成数组,指定每个元素的最大值
/// Effectively split integers into arrays, specifying the maximum value for each element ///
/// 整数信息 /// 单个的数组长度 /// 拆分后的数组长度 /// /// /// public static int[] SplitIntegerToArray(int integer, int everyLength) { int[] result = new int[(integer / everyLength) + ((integer % everyLength) == 0 ? 0 : 1)]; for (int i = 0; i < result.Length; i++) { if (i == result.Length - 1) { result[i] = (integer % everyLength) == 0 ? everyLength : (integer % everyLength); } else { result[i] = everyLength; } } return result; } #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) => IsTwoBytesEquel(head, 12, token.ToByteArray(), 0, 16); /// /// 判断两个数据的令牌是否相等
/// Determines whether the tokens of two data are equal ///
/// 第一个令牌 /// 第二个令牌 /// 返回是否相等 public static bool IsTwoTokenEquel(Guid token1, Guid token2) => 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 => (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 => (TEnum)Enum.Parse(typeof(TEnum), value); #endregion #region JSON XML Data Get /// /// 一个泛型方法,提供json对象的数据读取
/// A generic method that provides data read for a JSON object ///
/// 读取的泛型 /// json对象 /// 值名称 /// 默认值 /// 值对象 /// /// /// public static T GetValueFromJsonObject(JObject json, string name, T defaultValue) { if (json.Property(name) != null) { return json.Property(name).Value.Value(); } else { return defaultValue; } } /// /// 一个泛型方法,提供json对象的数据写入
/// A generic method that provides data writing to a JSON object ///
/// 写入的泛型 /// json对象 /// 值名称 /// 值数据 /// /// /// public static void JsonSetValue(JObject json, string property, T value) { if (json.Property(property) != null) { json.Property(property).Value = new JValue(value); } else { json.Add(property, new JValue(value)); } } #endregion /// /// 获取一个异常的完整错误信息
/// 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); } #region Hex string and Byte[] transform /// /// 字节数据转化成16进制表示的字符串
/// Byte data into a string of 16 binary representations ///
/// 字节数组 /// 返回的字符串 /// /// /// public static string ByteToHexString(byte[] InBytes) => ByteToHexString(InBytes, (char)0); /// /// 字节数据转化成16进制表示的字符串
/// Byte data into a string of 16 binary representations ///
/// 字节数组 /// 分割符 /// 返回的字符串 /// /// /// public static string ByteToHexString(byte[] InBytes, char segment) => ByteToHexString(InBytes, segment, 0); /// /// 字节数据转化成16进制表示的字符串
/// Byte data into a string of 16 binary representations ///
/// 字节数组 /// 分割符,如果设置为0,则没有分隔符信息 /// 每隔指定数量的时候进行换行,如果小于等于0,则不进行分行显示 /// 格式信息,默认为{0:X2} /// 返回的字符串 /// /// /// public static string ByteToHexString(byte[] InBytes, char segment, int newLineCount, string format = "{0:X2}") { if (InBytes == null) return string.Empty; StringBuilder sb = new StringBuilder(); long tick = 0; foreach (byte InByte in InBytes) { if (segment == 0) sb.Append(string.Format(format, InByte)); else sb.Append(string.Format(format + "{1}", InByte, segment)); tick++; if (newLineCount > 0 && tick >= newLineCount) { sb.Append(Environment.NewLine); tick = 0; } } 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) => ByteToHexString(Encoding.Unicode.GetBytes(InString)); private static int GetHexCharIndex(char ch) { switch (ch) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; case 'A': case 'a': return 10; case 'B': case 'b': return 11; case 'C': case 'c': return 12; case 'D': case 'd': return 13; case 'E': case 'e': return 14; case 'F': case 'f': return 15; default: return -1; } } /// /// 将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) { MemoryStream ms = new MemoryStream(); for (int i = 0; i < hex.Length; i++) { if ((i + 1) < hex.Length) { if (GetHexCharIndex(hex[i]) >= 0 && GetHexCharIndex(hex[i + 1]) >= 0) { // 这是一个合格的字节数据 ms.WriteByte((byte)(GetHexCharIndex(hex[i]) * 16 + GetHexCharIndex(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 ///
/// /// 例如传入的字节数据是 01 02 03 04, 那么反转后就是 02 01 04 03 /// /// 输入的字节信息 /// 反转后的数据 /// /// /// public static byte[] BytesReverseByWord(byte[] inBytes) { if (inBytes == null) return null; if (inBytes.Length == 0) return new byte[0]; byte[] buffer = ArrayExpandToLengthEven(CopyArray(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; } /// /// 拷贝当前的实例数组,是基于引用层的浅拷贝,如果类型为值类型,那就是深度拷贝,如果类型为引用类型,就是浅拷贝 /// /// 类型对象 /// 数组对象 /// 拷贝的结果内容 public static T[] CopyArray(T[] value) { if (value == null) return null; T[] buffer = new T[value.Length]; Array.Copy(value, buffer, value.Length); return buffer; } #endregion #region Byte[] and AsciiByte[] transform /// /// 将字节数组显示为ASCII格式的字符串,当遇到0x20以下及0x7E以上的不可见字符时,使用十六进制的数据显示
/// Display the byte array as a string in ASCII format, when encountering invisible characters below 0x20 and above 0x7E, use hexadecimal data to display
///
/// 字节数组信息 /// ASCII格式的字符串信息 public static string GetAsciiStringRender(byte[] content) { if (content == null) return string.Empty; StringBuilder sb = new StringBuilder(); for (int i = 0; i < content.Length; i++) { if (content[i] < 0x20 || content[i] > 0x7E) { sb.Append($"\\{content[i]:X2}"); } else { sb.Append((char)content[i]); } } return sb.ToString(); } /// /// 从显示的ASCII格式的字符串,转为原始字节数组,如果遇到 \00 这种表示原始字节的内容,则直接进行转换操作,遇到 \r 直接转换 0x0D, \n 直接转换 0x0A
/// Convert from the displayed string in ASCII format to the original byte array. If you encounter \00, which represents the original byte content, /// the conversion operation is performed directly. When encountering \r, it is directly converted to 0x0D, and \n is directly converted to 0x0A. ///
/// 等待转换的字符串 /// 原始字节数组 public static byte[] GetFromAsciiStringRender(string render) { if (string.IsNullOrEmpty(render)) return new byte[0]; MatchEvaluator matchEvaluator = new MatchEvaluator(m => string.Format("{0}", (char)Convert.ToByte(m.Value.Substring(1), 16))); return Encoding.ASCII.GetBytes(Regex.Replace(render.Replace("\\r", "\r").Replace("\\n", "\n"), @"\\[0-9A-Fa-f]{2}", matchEvaluator)); } /// /// 将原始的byte数组转换成ascii格式的byte数组
/// Converts the original byte array to an ASCII-formatted byte array ///
/// 等待转换的byte数组 /// 转换后的数组 public static byte[] BytesToAsciiBytes(byte[] inBytes) => 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) => HexStringToBytes(Encoding.ASCII.GetString(inBytes)); /// /// 从字节构建一个ASCII格式的数据内容
/// Build an ASCII-formatted data content from bytes ///
/// 数据 /// ASCII格式的字节数组 public static byte[] BuildAsciiBytesFrom(byte value) => Encoding.ASCII.GetBytes(value.ToString("X2")); /// /// 从short构建一个ASCII格式的数据内容
/// Constructing an ASCII-formatted data content from a short ///
/// 数据 /// ASCII格式的字节数组 public static byte[] BuildAsciiBytesFrom(short value) => Encoding.ASCII.GetBytes(value.ToString("X4")); /// /// 从ushort构建一个ASCII格式的数据内容
/// Constructing an ASCII-formatted data content from ushort ///
/// 数据 /// ASCII格式的字节数组 public static byte[] BuildAsciiBytesFrom(ushort value) => Encoding.ASCII.GetBytes(value.ToString("X4")); /// /// 从uint构建一个ASCII格式的数据内容
/// Constructing an ASCII-formatted data content from uint ///
/// 数据 /// ASCII格式的字节数组 public static byte[] BuildAsciiBytesFrom(uint value) => Encoding.ASCII.GetBytes(value.ToString("X8")); /// /// 从字节数组构建一个ASCII格式的数据内容
/// Byte array to construct an ASCII format data content ///
/// 字节信息 /// ASCII格式的地址 public static byte[] BuildAsciiBytesFrom(byte[] value) { byte[] buffer = new byte[value.Length * 2]; for (int i = 0; i < value.Length; i++) { SoftBasic.BuildAsciiBytesFrom(value[i]).CopyTo(buffer, 2 * i); } return buffer; } #endregion #region Bool and Byte transform private static byte GetDataByBitIndex(int offset) { switch (offset) { case 0: return 0x01; case 1: return 0x02; case 2: return 0x04; case 3: return 0x08; case 4: return 0x10; case 5: return 0x20; case 6: return 0x40; case 7: return 0x80; default: return 0; } } /// /// 获取byte数据类型的第offset位,是否为True
/// Gets the index bit of the byte data type, whether it is True ///
/// byte数值 /// 索引位置 /// 结果 /// /// /// public static bool BoolOnByteIndex(byte value, int offset) { byte temp = GetDataByBitIndex(offset); return (value & temp) == temp; } /// /// 设置取byte数据类型的第offset位,是否为True
/// Set the offset bit of the byte data type, whether it is True ///
/// byte数值 /// 索引位置 /// 写入的结果值 /// 结果 /// /// /// public static byte SetBoolOnByteIndex(byte byt, int offset, bool value) { byte temp = GetDataByBitIndex(offset); if (value) return (byte)(byt | temp); return (byte)(byt & (~temp)); } /// /// 将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++) { if (array[i]) buffer[i / 8] += GetDataByBitIndex(i % 8); } return buffer; } /// /// 将bool数组转换为字符串进行显示,true被转为1,false转换为0
/// Convert the bool array to a string for display, true is converted to 1, false is converted to 0 ///
/// bool数组 /// 转换后的字符串 public static string BoolArrayToString(bool[] array) { if (array == null) return string.Empty; StringBuilder sb = new StringBuilder(); for (int i = 0; i < array.Length; i++) { sb.Append(array[i] ? "1" : "0"); } return sb.ToString(); } /// /// 从Byte数组中提取位数组,length代表位数,例如数组 03 A1 长度10转为 [1100 0000 10]
/// 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++) { buffer[i] = BoolOnByteIndex(inBytes[i / 8], i % 8); } return buffer; } /// /// 从Byte数组中提取位数组,length代表位数,例如数组 01 00 长度10转为 [true,false]
///
/// 原先的字节数组 /// 想要转换的长度,如果超出自动会缩小到数组最大长度 /// 当为true时对应的byte值 /// 转换后bool数组 public static bool[] ByteToBoolArray(byte[] inBytes, int length, byte trueValue) { if (inBytes == null) return null; if (length > inBytes.Length) length = inBytes.Length; bool[] buffer = new bool[length]; for (int i = 0; i < length; i++) { buffer[i] = inBytes[i] == trueValue; } return buffer; } /// /// 从Byte数组中提取所有的位数组
/// Extracts a bit array from a byte array, length represents the number of digits ///
/// 原先的字节数组 /// 转换后的bool数组 /// /// /// public static bool[] ByteToBoolArray(byte[] InBytes) => InBytes == null ? null : ByteToBoolArray(InBytes, InBytes.Length * 8); #endregion #region Array Clip /// /// 将一个数组的前后移除指定位数,返回新的一个数组
/// Removes a array before and after the specified number of bits, returning a new array ///
/// 数组 /// 前面的位数 /// 后面的位数 /// 新的数组 /// /// /// /// public static T[] ArrayRemoveDouble(T[] value, int leftLength, int rightLength) { if (value == null) return null; if (value.Length <= (leftLength + rightLength)) return new T[0]; T[] buffer = new T[value.Length - leftLength - rightLength]; Array.Copy(value, leftLength, buffer, 0, buffer.Length); return buffer; } /// /// 将一个数组的前面指定位数移除,返回新的一个数组
/// Removes the preceding specified number of bits in a array, returning a new array ///
/// 数组 /// 等待移除的长度 /// 新的数组 /// /// /// /// public static T[] ArrayRemoveBegin(T[] value, int length) => ArrayRemoveDouble(value, length, 0); /// /// 将一个数组的后面指定位数移除,返回新的一个数组
/// Removes the specified number of digits after a array, returning a new array ///
/// 数组 /// 等待移除的长度 /// 新的数组 /// /// /// /// public static T[] ArrayRemoveLast(T[] value, int length) => ArrayRemoveDouble(value, 0, length); /// /// 获取到数组里面的中间指定长度的数组
/// Get an array of the specified length in the array ///
/// 数组 /// 起始索引 /// 数据的长度 /// 新的数组值 /// /// /// /// public static T[] ArraySelectMiddle(T[] value, int index, int length) { if (value == null) return null; if (length == 0) return new T[0]; T[] buffer = new T[Math.Min(value.Length, length)]; Array.Copy(value, index, buffer, 0, buffer.Length); return buffer; } /// /// 选择一个数组的前面的几个数据信息
/// Select the begin few items of data information of a array ///
/// 数组 /// 数据的长度 /// 新的数组 /// /// /// public static T[] ArraySelectBegin(T[] value, int length) { if (length == 0) return new T[0]; T[] buffer = new T[Math.Min(value.Length, length)]; if (buffer.Length > 0) Array.Copy(value, 0, buffer, 0, buffer.Length); return buffer; } /// /// 选择一个数组的后面的几个数据信息
/// Select the last few items of data information of a array ///
/// 数组 /// 数据的长度 /// 新的数组信息 /// /// /// public static T[] ArraySelectLast(T[] value, int length) { if (length == 0) return new T[0]; T[] buffer = new T[Math.Min(value.Length, length)]; Array.Copy(value, value.Length - length, buffer, 0, buffer.Length); return buffer; } #endregion #region Byte[] Splice /// /// 拼接任意个泛型数组为一个总的泛型数组对象,采用深度拷贝实现。
/// Splicing any number of generic arrays into a total generic array object is implemented using deep copy. ///
/// 数组的类型信息 /// 任意个长度的数组 /// 拼接之后的最终的结果对象 /// /// /// public static T[] SpliceArray(params T[][] arrays) { int count = 0; for (int i = 0; i < arrays.Length; i++) { if (arrays[i]?.Length > 0) { count += arrays[i].Length; } } int index = 0; T[] buffer = new T[count]; for (int i = 0; i < arrays.Length; i++) { if (arrays[i]?.Length > 0) { arrays[i].CopyTo(buffer, index); index += arrays[i].Length; } } return buffer; } #endregion #region String Array Splice /// /// 将一个的数组和多个 类型的对象整合成一个数组
/// Combine an array of and multiple objects of type into an array ///
/// 第一个数组对象 /// 字符串数组信息 /// 总的数组对象 public static string[] SpliceStringArray(string first, string[] array) { List list = new List(); list.Add(first); list.AddRange(array); return list.ToArray(); } /// /// 将两个的数组和多个 类型的对象整合成一个数组
/// Combine two arrays of and multiple objects of type into one array ///
/// 第一个数据对象 /// 第二个数据对象 /// 字符串数组信息 /// 总的数组对象 public static string[] SpliceStringArray(string first, string second, string[] array) { List list = new List(); list.Add(first); list.Add(second); list.AddRange(array); return list.ToArray(); } /// /// 将两个的数组和多个 类型的对象整合成一个数组
/// Combine two arrays of and multiple objects of type into one array ///
/// 第一个数据对象 /// 第二个数据对象 /// 第三个数据对象 /// 字符串数组信息 /// 总的数组对象 public static string[] SpliceStringArray(string first, string second, string third, string[] array) { List list = new List(); list.Add(first); list.Add(second); list.Add(third); list.AddRange(array); return list.ToArray(); } #endregion #region Url Encode Decode // 以下代码来自于微软自身的开源库 private static int HexToInt(char h) { return (h >= '0' && h <= '9') ? h - '0' : (h >= 'a' && h <= 'f') ? h - 'a' + 10 : (h >= 'A' && h <= 'F') ? h - 'A' + 10 : -1; } private static string ValidateString(string input, bool skipUtf16Validation) { if (skipUtf16Validation || String.IsNullOrEmpty(input)) { return input; } // locate the first surrogate character int idxOfFirstSurrogate = -1; for (int i = 0; i < input.Length; i++) { if (Char.IsSurrogate(input[i])) { idxOfFirstSurrogate = i; break; } } // fast case: no surrogates = return input string if (idxOfFirstSurrogate < 0) { return input; } // slow case: surrogates exist, so we need to validate them char[] chars = input.ToCharArray(); for (int i = idxOfFirstSurrogate; i < chars.Length; i++) { char thisChar = chars[i]; // If this character is a low surrogate, then it was not preceded by // a high surrogate, so we'll replace it. if (Char.IsLowSurrogate(thisChar)) { chars[i] = '\uFFFD'; continue; } if (Char.IsHighSurrogate(thisChar)) { // If this character is a high surrogate and it is followed by a // low surrogate, allow both to remain. if (i + 1 < chars.Length && Char.IsLowSurrogate(chars[i + 1])) { i++; // skip the low surrogate also continue; } // If this character is a high surrogate and it is not followed // by a low surrogate, replace it. chars[i] = '\uFFFD'; continue; } // Otherwise, this is a non-surrogate character and just move to the // next character. } return new String(chars); } private static bool ValidateUrlEncodingParameters(byte[] bytes, int offset, int count) { if (bytes == null && count == 0) return false; if (bytes == null) { throw new ArgumentNullException("bytes"); } if (offset < 0 || offset > bytes.Length) { throw new ArgumentOutOfRangeException("offset"); } if (count < 0 || offset + count > bytes.Length) { throw new ArgumentOutOfRangeException("count"); } return true; } private static bool IsUrlSafeChar(char ch) { if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9')) return true; switch (ch) { case '-': case '_': case '.': case '!': case '*': case '(': case ')': return true; } return false; } private static string UrlEncodeSpaces(string str) { if (str != null && str.IndexOf(' ') >= 0) str = str.Replace(" ", "%20"); return str; } private static char IntToHex(int n) { Debug.Assert(n < 0x10); if (n <= 9) return (char)(n + (int)'0'); else return (char)(n - 10 + (int)'A'); } private static byte[] UrlEncodeToBytes(byte[] bytes) { int offset = 0; int count = bytes.Length; if (!ValidateUrlEncodingParameters(bytes, offset, count)) { return null; } int cSpaces = 0; int cUnsafe = 0; // count them first for (int i = 0; i < count; i++) { char ch = (char)bytes[offset + i]; if (ch == ' ') cSpaces++; else if (!IsUrlSafeChar(ch)) cUnsafe++; } // nothing to expand? if (cSpaces == 0 && cUnsafe == 0) { // DevDiv 912606: respect "offset" and "count" if (0 == offset && bytes.Length == count) { return bytes; } else { var subarray = new byte[count]; Buffer.BlockCopy(bytes, offset, subarray, 0, count); return subarray; } } // expand not 'safe' characters into %XX, spaces to +s byte[] expandedBytes = new byte[count + cUnsafe * 2]; int pos = 0; for (int i = 0; i < count; i++) { byte b = bytes[offset + i]; char ch = (char)b; if (IsUrlSafeChar(ch)) { expandedBytes[pos++] = b; } else if (ch == ' ') { expandedBytes[pos++] = (byte)'+'; } else { expandedBytes[pos++] = (byte)'%'; expandedBytes[pos++] = (byte)IntToHex((b >> 4) & 0xf); expandedBytes[pos++] = (byte)IntToHex(b & 0x0f); } } return expandedBytes; } private static byte[] UrlEncode(byte[] bytes, bool alwaysCreateNewReturnValue) { byte[] encoded = UrlEncodeToBytes(bytes); return (alwaysCreateNewReturnValue && (encoded != null) && (encoded == bytes)) ? (byte[])encoded.Clone() : encoded; } /// /// 将字符串编码为URL可以识别的字符串,中文会被编码为%开头的数据,例如 中文 转义为 %2F%E4%B8%AD%E6%96%87
/// Encoding a string as a URL-recognizable string Chinese encoded as data that begins with %, such as 中文 escaped as %2F%E4%B8%AD%E6%96%87 ///
/// 等待转换的字符串数据 /// 编码信息,一般为 UTF8 /// 编码之后的结果 public static string UrlEncode(string str, Encoding e) { if (str == null) return null; byte[] bytes = e.GetBytes(str); return Encoding.ASCII.GetString(UrlEncode(bytes, true)); } /// /// 将url的编码解码为真实的字符串,例如 %2F%E4%B8%AD%E6%96%87 解码为 中文
/// Decode the encoding of url as a real string, for example %2F%E4%B8%AD%E6%96%87 to 中文 ///
/// 等待转换的值 /// 编码信息,一般为 UTF8 /// 解码之后的结果 public static string UrlDecode(string value, Encoding encoding) { int count = value.Length; UrlDecoder helper = new UrlDecoder(count, encoding); // go through the string's chars collapsing %XX and %uXXXX and // appending each char as char, with exception of %XX constructs // that are appended as bytes for (int pos = 0; pos < count; pos++) { char ch = value[pos]; if (ch == '+') { ch = ' '; } else if (ch == '%' && pos < count - 2) { if (value[pos + 1] == 'u' && pos < count - 5) { int h1 = HexToInt(value[pos + 2]); int h2 = HexToInt(value[pos + 3]); int h3 = HexToInt(value[pos + 4]); int h4 = HexToInt(value[pos + 5]); if (h1 >= 0 && h2 >= 0 && h3 >= 0 && h4 >= 0) { // valid 4 hex chars ch = (char)((h1 << 12) | (h2 << 8) | (h3 << 4) | h4); pos += 5; // only add as char helper.AddChar(ch); continue; } } else { int h1 = HexToInt(value[pos + 1]); int h2 = HexToInt(value[pos + 2]); if (h1 >= 0 && h2 >= 0) { // valid 2 hex chars byte b = (byte)((h1 << 4) | h2); pos += 2; // don't add as char helper.AddByte(b); continue; } } } if ((ch & 0xFF80) == 0) helper.AddByte((byte)ch); // 7 bit have to go as bytes because of Unicode else helper.AddChar(ch); } return ValidateString(helper.GetString(), true); } #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() { return Guid.NewGuid().ToString("N") + new Random().Next(1000, 10000); } #endregion } class UrlDecoder { private int _bufferSize; // Accumulate characters in a special array private int _numChars; private char[] _charBuffer; // Accumulate bytes for decoding into characters in a special array private int _numBytes; private byte[] _byteBuffer; // Encoding to convert chars to bytes private Encoding _encoding; private void FlushBytes() { if (_numBytes > 0) { _numChars += _encoding.GetChars(_byteBuffer, 0, _numBytes, _charBuffer, _numChars); _numBytes = 0; } } internal UrlDecoder(int bufferSize, Encoding encoding) { _bufferSize = bufferSize; _encoding = encoding; _charBuffer = new char[bufferSize]; // byte buffer created on demand } internal void AddChar(char ch) { if (_numBytes > 0) FlushBytes(); _charBuffer[_numChars++] = ch; } internal void AddByte(byte b) { // if there are no pending bytes treat 7 bit bytes as characters // this optimization is temp disable as it doesn't work for some encodings /* if (_numBytes == 0 && ((b & 0x80) == 0)) { AddChar((char)b); } else */ { if (_byteBuffer == null) _byteBuffer = new byte[_bufferSize]; _byteBuffer[_numBytes++] = b; } } internal string GetString() { if (_numBytes > 0) FlushBytes(); if (_numChars > 0) return new string(_charBuffer, 0, _numChars); else return string.Empty; } } }