Пример #1
0
        private static string ConvertToSignedDecimal(DataValue value)
        {
            var isNegative = false;

            BitValue[] bitValue = value.CopyBitValues();
            if (bitValue[bitValue.Length - 1] == BitValue.One)
            {
                isNegative = true;
                bitValue.Negate(ref bitValue);
            }

            long total = RadixTypeExtensions.GetUnsignedTotal(bitValue);

            switch (total)
            {
            case RadixTypeExtensions.FLOATING:
                return("X");

            case RadixTypeExtensions.ERROR:
                return("*");

            default:
                return(isNegative ? $"-{total}" : total.ToString());
            }
        }
Пример #2
0
        private static string ConvertToHex(DataValue value)
        {
            var builder = new StringBuilder();

            var hexBit   = 0;
            var hexTotal = 0;

            for (var i = 0; i < value.BitWidth; i++)
            {
                if (hexBit == 4)
                {
                    builder.Insert(0, RadixTypeExtensions.ToHexString(hexTotal));
                    hexBit   = 0;
                    hexTotal = 0;
                }

                switch (value[i])
                {
                case BitValue.One:
                    hexTotal += 1 << hexBit++;
                    break;

                case BitValue.Zero:
                    hexBit++;
                    break;

                case BitValue.Error:
                    builder.Insert(0, "*");
                    i       += 4 - hexBit;
                    hexBit   = 0;
                    hexTotal = 0;
                    break;

                case BitValue.Floating:
                    var hasError = false;
                    for (int f = hexBit; (f < 4) && (f + i < value.BitWidth); f++)
                    {
                        hasError = hasError || (value[f + i] == BitValue.Error);
                    }

                    builder.Insert(0, hasError ? "*" : "X");
                    i       += 4 - hexBit;
                    hexBit   = 0;
                    hexTotal = 0;
                    break;
                }
            }

            if (hexBit != 0)
            {
                builder.Insert(0, RadixTypeExtensions.ToHexString(hexTotal));
            }

            builder.Insert(0, "0x");

            return(builder.ToString());
        }
Пример #3
0
        private static string ConvertToUnsignedDecimal(DataValue value)
        {
            long total = RadixTypeExtensions.GetUnsignedTotal(value.CopyBitValues());

            switch (total)
            {
            case RadixTypeExtensions.FLOATING:
                return("X");

            case RadixTypeExtensions.ERROR:
                return("*");

            default:
                return(total.ToString());
            }
        }
Пример #4
0
        /// <summary>
        /// Converts the specified <see cref="DataValue" /> to a string using the given <see cref="RadixType" /> .
        /// </summary>
        /// <param name="radix">The <see cref="RadixType" /> .</param>
        /// <param name="value">The value.</param>
        /// <returns>
        /// A string of the <see cref="DataValue" /> interpreted using the given <see cref="RadixType" /> .
        /// </returns>
        public static string Convert(this RadixType radix, DataValue value)
        {
            switch (radix)
            {
            case RadixType.Binary:
                return(RadixTypeExtensions.ConvertToBinary(value));

            case RadixType.Octal:
                return(RadixTypeExtensions.ConvertToOctal(value));

            case RadixType.SignedDecimal:
                return(RadixTypeExtensions.ConvertToSignedDecimal(value));

            case RadixType.UnsignedDecimal:
                return(RadixTypeExtensions.ConvertToUnsignedDecimal(value));

            case RadixType.Hex:
                return(RadixTypeExtensions.ConvertToHex(value));

            default:
                throw new NotImplementedException("Unknown Radix Type.");
            }
        }