StringHelper.cs 4.3 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DL.Utils
{
    public class StringHelper
    {
        /// <summary>
        /// 字节转十六进制字符串
        /// </summary>
        /// <param name="buff"></param>
        /// <returns></returns>
        public static string ToHexString(byte[] buff)
        {
            StringBuilder sb = new StringBuilder("");
            if (buff == null || buff.Length<1) return sb.ToString();
            for (int i = 0; i < buff.Length; i++)
            {
                sb.Append(buff[i].ToString("X2"));
                sb.Append(" ");
            }
            sb.Remove(sb.Length - 1, 1);
            return sb.ToString();
        }
        public static string ToBinaryString(byte[] data)
        {
            StringBuilder stringBuilder = new StringBuilder("");
            if (data == null || data.Length < 1) return stringBuilder.ToString();
            stringBuilder.Append("【");
            foreach (var item in data)
            {
                stringBuilder.Append(Convert.ToString(item, 2).PadLeft(8, '0'));
                stringBuilder.Append(" ");
            }
            stringBuilder.Remove(stringBuilder.Length - 1, 1);
            stringBuilder.Append("】");
            return stringBuilder.ToString();
        }
        public static string ToHexString(byte data)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.Append("【");
            stringBuilder.Append(data.ToString("x2"));
            stringBuilder.Append("】");
            return stringBuilder.ToString();
        }
        
        public static byte[] StrToHexByte(string hexString)
        {
            hexString = hexString.Replace(" ", "");
            if ((hexString.Length % 2) != 0)
                hexString += " ";
            byte[] returnBytes = new byte[hexString.Length / 2];
            for (int i = 0; i < returnBytes.Length; i++)
            {
                returnBytes[i]=Convert.ToByte(hexString.Substring(i*2,2),16);
            }
            return returnBytes;
        }
        /// <summary>
        /// 获取类的属性名及其值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <returns></returns>
        public static string GetPropertiesStr<T>(T t)
        {
            StringBuilder sb = new StringBuilder("");
            if (t == null)
            {
                return sb.ToString();
            }
            System.Reflection.PropertyInfo[] properties = t.GetType().GetProperties(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);

            if (properties.Length <= 0)
            {
                return sb.ToString();
            }
            sb.Append("{");
            foreach (System.Reflection.PropertyInfo property in properties)
            {
                string name = property.Name;
                object value = property.GetValue(t, null);
                if (property.PropertyType.IsValueType || property.PropertyType.Name.StartsWith("String"))
                {
                    sb.Append($"{name}:{value},");
                }
                else if(property.PropertyType.IsGenericType)
                {
                    var listVal = property.GetValue(t, null) as IEnumerable<object>;
                    if (listVal == null) continue;
                    sb.Append("[");
                    foreach (var item1 in listVal)
                    {
                        sb.Append(GetPropertiesStr(item1));
                    }
                    sb.Append("]");
                }
                else if(property.PropertyType.IsArray)
                {
                    var listVal = property.GetValue(t, null) as IEnumerable<object>;
                    if (listVal == null) continue;
                    sb.Append("[");
                    foreach (var item1 in listVal)
                    {
                        sb.Append(GetPropertiesStr(item1));
                    }
                    sb.Append("]");
                }
                else
                {
                   sb.Append(GetPropertiesStr(value));
                }
            }
            sb.Append("}");
            return sb.ToString();
        }
    }
}