Exemple #1
0
    private void ConvertSingleNum(string str)
    {
        int len = CheckStorageSpace(str);

        if (len < 1)
        {
            AddToOutput("<color #ff0000>ERROR: input str is illegal!</color>");
            return;
        }

        byte[] byteArr = new byte[len];

        if (inputBaseMode == InputBaseMode.BIN)
        {
            if (ConvertHelper.IsByteRange(str))
            {
                byteArr[0] = BinStr2Byte(str);
            }
            else if (ConvertHelper.IsInt16Range(str))
            {
                byteArr = BitConverter.GetBytes(BinStr2Int16(str));
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(byteArr);
                }
            }
            else if (ConvertHelper.IsInt32Range(str))
            {
                byteArr = BitConverter.GetBytes(BinStr2Int32(str));
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(byteArr);
                }
            }
        }
        else if (inputBaseMode == InputBaseMode.DEC)
        {
            byte  res_Byte  = 0;
            short res_Short = 0;
            int   res_Int   = 0;

            if (ConvertHelper.IsByteRange(str, out res_Byte))
            {
                byteArr[0] = res_Byte;
            }
            else if (ConvertHelper.IsInt16Range(str, out res_Short))
            {
                var byArr_Int16 = BitConverter.GetBytes(res_Short);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(byArr_Int16);
                }

                Buffer.BlockCopy(byArr_Int16, 0, byteArr, 0, byArr_Int16.Length);
            }
            else if (ConvertHelper.IsInt32Range(str, out res_Int))
            {
                var byArr_Int32 = BitConverter.GetBytes(res_Int);
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(byArr_Int32);
                }

                Buffer.BlockCopy(byArr_Int32, 0, byteArr, 0, byArr_Int32.Length);
            }
        }
        else if (inputBaseMode == InputBaseMode.HEX)
        {
            if (ConvertHelper.IsByteRange(str))
            {
                byteArr[0] = HexStr2Byte(str);
            }
            else if (ConvertHelper.IsInt16Range(str))
            {
                byteArr = BitConverter.GetBytes(HexStr2Int16(str));
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(byteArr);
                }
            }
            else if (ConvertHelper.IsInt32Range(str))
            {
                byteArr = BitConverter.GetBytes(HexStr2Int32(str));
                if (BitConverter.IsLittleEndian)
                {
                    Array.Reverse(byteArr);
                }
            }
        }

        if (outputBaseMode == OutputBaseMode.BIN)
        {
            AddToOutput(ConvertHelper.ByteArrToBin(byteArr));
        }
        else if (outputBaseMode == OutputBaseMode.DEC)
        {
            if (ConvertHelper.IsByteRange(str))
            {
                AddToOutput(ConvertHelper.ByteArrToByte(byteArr));
            }
            else if (ConvertHelper.IsInt16Range(str))
            {
                AddToOutput(ConvertHelper.ByteArrToInt16(byteArr));
            }
            else if (ConvertHelper.IsInt32Range(str))
            {
                AddToOutput(ConvertHelper.ByteArrToInt32(byteArr));
            }
        }
        else if (outputBaseMode == OutputBaseMode.HEX)
        {
            AddToOutput(ConvertHelper.ByteArrToHex(byteArr));
        }
    }