コード例 #1
0
ファイル: HexUtilities.cs プロジェクト: sticks-stuff/STROOP
        public static string FormatValue(object number, int?numDigits = null, bool usePrefix = true)
        {
            object numberFormatted = number;

            // Make sure it's a number
            if (!TypeUtilities.IsNumber(numberFormatted))
            {
                numberFormatted = ParsingUtilities.ParseDoubleNullable(numberFormatted);
                if (numberFormatted == null)
                {
                    return(number.ToString());
                }
            }

            // Convert floats/doubles into ints/uints
            if (numberFormatted is float || numberFormatted is double)
            {
                if (numberFormatted is float floatValue)
                {
                    numberFormatted = Math.Round(floatValue);
                }
                if (numberFormatted is double doubleValue)
                {
                    numberFormatted = Math.Round(doubleValue);
                }

                int?intValueNullable = ParsingUtilities.ParseIntNullable(numberFormatted);
                if (intValueNullable.HasValue)
                {
                    numberFormatted = intValueNullable.Value;
                }
                uint?uintValueNullable = ParsingUtilities.ParseUIntNullable(numberFormatted);
                if (uintValueNullable.HasValue)
                {
                    numberFormatted = uintValueNullable.Value;
                }
            }

            if (!TypeUtilities.IsIntegerNumber(numberFormatted))
            {
                return(number.ToString());
            }

            string numDigitsString = numDigits.HasValue ? numDigits.Value.ToString() : "";
            string hexString       = String.Format("{0:X" + numDigitsString + "}", numberFormatted);
            string prefix          = usePrefix ? "0x" : "";

            if (numDigits.HasValue)
            {
                hexString = StringUtilities.ExactLength(hexString, numDigits.Value, true, '0');
            }
            return(prefix + hexString);
        }
コード例 #2
0
      public bool SetValue(Type type, object value, uint address, bool absoluteAddress = false, uint?mask = null, int?shift = null)
      {
          if (value is string)
          {
              if (type == typeof(byte))
              {
                  value = ParsingUtilities.ParseByteNullable(value);
              }
              if (type == typeof(sbyte))
              {
                  value = ParsingUtilities.ParseSByteNullable(value);
              }
              if (type == typeof(short))
              {
                  value = ParsingUtilities.ParseShortNullable(value);
              }
              if (type == typeof(ushort))
              {
                  value = ParsingUtilities.ParseUShortNullable(value);
              }
              if (type == typeof(int))
              {
                  value = ParsingUtilities.ParseIntNullable(value);
              }
              if (type == typeof(uint))
              {
                  value = ParsingUtilities.ParseUIntNullable(value);
              }
              if (type == typeof(float))
              {
                  value = ParsingUtilities.ParseFloatNullable(value);
              }
              if (type == typeof(double))
              {
                  value = ParsingUtilities.ParseDoubleNullable(value);
              }
          }

          if (value == null)
          {
              return(false);
          }

          if (type == typeof(byte))
          {
              return(SetValue((byte)value, address, absoluteAddress, mask, shift));
          }
          if (type == typeof(sbyte))
          {
              return(SetValue((sbyte)value, address, absoluteAddress, mask, shift));
          }
          if (type == typeof(short))
          {
              return(SetValue((short)value, address, absoluteAddress, mask, shift));
          }
          if (type == typeof(ushort))
          {
              return(SetValue((ushort)value, address, absoluteAddress, mask, shift));
          }
          if (type == typeof(int))
          {
              return(SetValue((int)value, address, absoluteAddress, mask, shift));
          }
          if (type == typeof(uint))
          {
              return(SetValue((uint)value, address, absoluteAddress, mask, shift));
          }
          if (type == typeof(float))
          {
              return(SetValue((float)value, address, absoluteAddress, mask, shift));
          }
          if (type == typeof(double))
          {
              return(SetValue((double)value, address, absoluteAddress, mask, shift));
          }

          throw new ArgumentOutOfRangeException("Cannot call ProcessStream.SetValue with type " + type);
      }