Esempio n. 1
0
        /// <summary>
        /// 将字节数组转换为指定进制的可读字符串
        /// </summary>
        /// <param name="yourBytes">需要转换的字节数组</param>
        /// <param name="hexDecimal">指定进制</param>
        /// <param name="stringMode">指定格式</param>
        /// <returns>返回结果</returns>
        public static string ByteToHexString(byte[] yourBytes, HexaDecimal hexDecimal, ShowHexMode stringMode)
        {
            // 如果只考虑16进制对格式没有特殊要求 可以直接使用 ((byte)233).ToString("X2"); 或 BitConverter.ToString(new byte[]{1,2,3,10,12,233})
            if (yourBytes == null)
            {
                return(null);
            }
            StringBuilder result = new StringBuilder(DictionaryHexaDecimal[hexDecimal] + DictionaryShowHexMode[stringMode].Length);

            for (int i = 0; i < yourBytes.Length; i++)
            {
                result.Append(DictionaryShowHexMode[stringMode]);
                result.Append(Convert.ToString(yourBytes[i], (int)hexDecimal).PadLeft(DictionaryHexaDecimal[hexDecimal], '0'));
            }
            return(result.ToString().Trim());
        }
Esempio n. 2
0
        /// <summary>
        /// 将可读指定进制的数据转换为字节数组(因为用户数据可能会是非法数据,遇到非法数据非法会抛出异常)
        /// </summary>
        /// <param name="yourStr">需要转换的字符串</param>
        /// <param name="hexDecimal">指定进制</param>
        /// <param name="stringMode">指定格式</param>
        /// <returns>返回结果(如果yourStr为""返回长度为0的byte[])</returns>
        public static byte[] HexStringToByte(string yourStr, HexaDecimal hexDecimal, ShowHexMode stringMode)
        {
            if (hexDecimal == HexaDecimal.hex16 && (stringMode == ShowHexMode.spit0b || stringMode == ShowHexMode.spit0d || stringMode == ShowHexMode.spitSpace0b || stringMode == ShowHexMode.spitSpace0d))
            {
                throw new Exception("your HexaDecimal and ShowHexMode is conflict");
            }
            string[] hexStrs;
            byte[]   resultBytes;
            string   modeStr = string.Empty; //string.Empty 不等于 null

            if (stringMode != ShowHexMode.@null)
            {
                modeStr = DictionaryShowHexMode[stringMode];
            }
            if (modeStr == string.Empty)
            {
                if (yourStr.Length % DictionaryHexaDecimal[hexDecimal] != 0)
                {
                    throw new Exception("error leng of your data");
                }
                long tempHexNum = yourStr.Length / DictionaryHexaDecimal[hexDecimal];
                hexStrs = new string[tempHexNum];
                for (int startIndex = 0; startIndex < tempHexNum; startIndex++)
                {
                    hexStrs[startIndex] = yourStr.Substring(startIndex * DictionaryHexaDecimal[hexDecimal], DictionaryHexaDecimal[hexDecimal]);
                }
            }
            else
            {
                //使用StringSplitOptions.RemoveEmptyEntries去除空值因为0xFF0xFF可能会有第一个值为空的情况
                //对于0xFF 0xFF使用 0x分割,第一个值可能会带0x,不过 Convert.ToByte可以兼容这种情况(不过还是可能带来第一个字节允许使用不应用的分割的情况)
                hexStrs = yourStr.Split(new string[] { modeStr }, StringSplitOptions.RemoveEmptyEntries);
            }
            try
            {
                resultBytes = new byte[hexStrs.Length];
                for (int i = 0; i < hexStrs.Length; i++)
                {
                    resultBytes[i] = Convert.ToByte(hexStrs[i], (int)hexDecimal);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("error data ,can not change your hex string to your hex,  hexDecimal[{0}] ShowHexMode[{1}]", hexDecimal, stringMode), ex);
            }
            return(resultBytes);
        }
Esempio n. 3
0
 /// <summary>
 /// 将可读指定进制的数据转换为字节数组(因为用户数据可能会是非法数据,遇到非法数据非法会抛出异常)(使用猜测的方式发现分割字符串)
 /// </summary>
 /// <param name="yourStr">需要转换的字符串</param>
 /// <param name="hexDecimal">指定进制</param>
 /// <returns>返回结果(如果yourStr为""返回长度为0的byte[])</returns>
 public static byte[] HexStringToByte(string yourStr, HexaDecimal hexDecimal)
 {
     if (yourStr == null)
     {
         throw new Exception("your source string is null");
     }
     foreach (var tryStringSpitMode in DictionaryShowHexMode)
     {
         if (hexDecimal == HexaDecimal.hex16 && (tryStringSpitMode.Key == ShowHexMode.spit0b || tryStringSpitMode.Key == ShowHexMode.spit0d || tryStringSpitMode.Key == ShowHexMode.spitSpace0b || tryStringSpitMode.Key == ShowHexMode.spitSpace0d))
         {
             continue;
         }
         if (yourStr.Contains(tryStringSpitMode.Value))
         {
             return(HexStringToByte(yourStr, hexDecimal, tryStringSpitMode.Key));
         }
     }
     throw new Exception("unknown exception with HexStringToByte");
 }
Esempio n. 4
0
        /// <summary>
        /// 将可读指定进制的数据转换为字节数组(因为用户数据可能会是非法数据,遇到非法数据非法会抛出异常)
        /// </summary>
        /// <param name="yourStr">需要转换的字符串</param>
        /// <param name="hexDecimal">指定进制</param>
        /// <param name="stringMode">指定格式</param>
        /// <returns>返回结果</returns>
        public static byte[] HexStringToByte(string yourStr, HexaDecimal hexDecimal, ShowHexMode stringMode)
        {
            string[] hexStrs;
            byte[]   resultBytes;
            string   modeStr = string.Empty; //string.Empty 不等于 null

            if (stringMode != ShowHexMode.@null)
            {
                modeStr = DictionaryShowHexMode[stringMode];
            }
            if (modeStr == string.Empty)
            {
                if (yourStr.Length % DictionaryHexaDecimal[hexDecimal] != 0)
                {
                    throw new Exception("error leng of your data");
                }
                long tempHexNum = yourStr.Length / DictionaryHexaDecimal[hexDecimal];
                hexStrs = new string[tempHexNum];
                for (int startIndex = 0; startIndex < tempHexNum; startIndex++)
                {
                    hexStrs[startIndex] = yourStr.Substring(startIndex * DictionaryHexaDecimal[hexDecimal], DictionaryHexaDecimal[hexDecimal]);
                }
            }
            else
            {
                hexStrs = yourStr.Split(new string[] { modeStr }, StringSplitOptions.RemoveEmptyEntries);
            }
            try
            {
                resultBytes = new byte[hexStrs.Length];
                for (int i = 0; i < hexStrs.Length; i++)
                {
                    resultBytes[i] = Convert.ToByte(hexStrs[i], (int)hexDecimal);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("error data ,can not change it to your hex", ex);
            }
            return(resultBytes);
        }
Esempio n. 5
0
 /// <summary>
 /// 将字符串转换成指定进制的可读字符串 (使用指定编码指定进制及指定格式)
 /// </summary>
 /// <param name="yourStr">用户字符串</param>
 /// <param name="encode">指定编码</param>
 /// <param name="hexaDecimal">指定进制</param>
 /// <param name="stringMode">指定格式</param>
 /// <returns>返回结果</returns>
 public static string StringToHexString(string yourStr, Encoding encode, HexaDecimal hexaDecimal, ShowHexMode stringMode)
 {
     byte[] tempBytes = encode.GetBytes(yourStr);
     return(ByteToHexString(tempBytes, hexaDecimal, stringMode));
 }