Exemple #1
0
        public void ArrayToHexDecimal()
        {
            var rand = new Random();

            var buf = new byte[12];

            rand.NextBytes(buf);
            var hl = new HexDecimal(buf, 0, buf.Length);
            var t  = hl.Value;
        }
Exemple #2
0
        public void DecimalToHexDecimal()
        {
            decimal t = -1;

            for (int i = 0; i < 96; i++)
            {
                t++;
                t *= 2;
                var hl = new HexDecimal(t);
                Console.WriteLine($"{hl} | {Hex.ToString(hl.Bytes)}");
                var it = hl.Value;
                Assert.IsTrue(t == it, $"{t} != {it}");
            }
        }
Exemple #3
0
        /// <summary>
        /// 将字节数组转换为指定进制的可读字符串
        /// </summary>
        /// <param name="yourBytes">需要转换的字节数组</param>
        /// <param name="hexDecimal">指定进制</param>
        /// <param name="stringMode">指定格式</param>
        /// <returns>返回结果</returns>
        public static string ByteToHexString(byte[] yourBytes, HexDecimal 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(DictionaryHexDecimal[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(DictionaryHexDecimal[hexDecimal], '0'));
            }
            return(result.ToString().Trim());
        }
Exemple #4
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, HexDecimal hexDecimal, ShowHexMode stringMode)
        {
            if (hexDecimal == HexDecimal.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 % DictionaryHexDecimal[hexDecimal] != 0)
                {
                    throw new Exception("error leng of your data");
                }
                long tempHexNum = yourStr.Length / DictionaryHexDecimal[hexDecimal];
                hexStrs = new string[tempHexNum];
                for (int startIndex = 0; startIndex < tempHexNum; startIndex++)
                {
                    hexStrs[startIndex] = yourStr.Substring(startIndex * DictionaryHexDecimal[hexDecimal], DictionaryHexDecimal[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);
        }
Exemple #5
0
        public void ArrayToHexDecimalInvalidCast()
        {
            var rand = new Random();

            var buf = new byte[32];

            rand.NextBytes(buf);
            try
            {
                var hl = new HexDecimal(buf, 0, buf.Length);
            }
            catch (InvalidCastException e)
            {
                Assert.Pass(e.Message);
                return;
            }
            Assert.Fail("expected exception");
        }
 public void Bytes32ToString(byte[] source)
 {
     if (source.Length == 32 + 32 + 32)
     {
         var len = new HexDecimal(source, 20, 12);
         if (len.Value == 32)
         {
             var    count = new HexDecimal(source, 32 + 20, 12);
             string utf8  = Encoding.UTF8.GetString(source, 64, 32);
             Console.WriteLine(utf8.Substring(0, (int)count.Value));
         }
         else
         {
             Console.WriteLine(Hex.ToString(source));
             throw new NotImplementedException();
         }
     }
 }
Exemple #7
0
 /// <summary>
 /// 将可读指定进制的数据转换为字节数组(因为用户数据可能会是非法数据,遇到非法数据非法会抛出异常)(使用猜测的方式发现分割字符串)
 /// </summary>
 /// <param name="yourStr">需要转换的字符串</param>
 /// <param name="hexDecimal">指定进制</param>
 /// <returns>返回结果(如果yourStr为""返回长度为0的byte[])</returns>
 public static byte[] HexStringToByte(string yourStr, HexDecimal hexDecimal)
 {
     if (yourStr == null)
     {
         throw new Exception("your source string is null");
     }
     foreach (var tryStringSpitMode in DictionaryShowHexMode)
     {
         if (hexDecimal == HexDecimal.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");
 }
Exemple #8
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, HexDecimal hexaDecimal, ShowHexMode stringMode)
 {
     byte[] tempBytes = encode.GetBytes(yourStr);
     return(ByteToHexString(tempBytes, hexaDecimal, stringMode));
 }