Пример #1
0
        public static DBValue FromRaw(byte[] data)
        {
            if (data == null)
            {
                return(null);
            }
            DBValue v = new DBValue();

            //read type
            v.type = (Type)data[0];
            //read tag
            v.tag = new byte[data[1]];
            for (var i = 0; i < v.tag.Length; i++)
            {
                v.tag[i] = data[i + 2];
            }
            //read last
            v.LastModifyHeight = BitConverter.ToUInt64(data, 2 + v.tag.Length);

            //read value
            v.value = new byte[data.Length - 2 - v.tag.Length - 8];
            for (var i = 0; i < v.value.Length; i++)
            {
                v.value[i] = data[2 + v.tag.Length + 8 + i];
            }
            v.ParseValue();
            return(v);
        }
Пример #2
0
        public static DBValue FromValue(Type _type, object _value)
        {
            DBValue v = new DBValue();

            v.type = _type;
            v.tag  = new byte[0];
            if (v.type == Type.Bytes && _value is byte[])
            {
            }
            else if (v.type == Type.INT32 && _value is Int32)
            {
                v.value = BitConverter.GetBytes((Int32)_value);
            }
            else if (v.type == Type.UINT32 && _value is UInt32)
            {
                v.value = BitConverter.GetBytes((UInt32)_value);
            }
            else if (v.type == Type.INT64 && _value is Int64)
            {
                v.value = BitConverter.GetBytes((Int64)_value);
            }
            else if (v.type == Type.UINT64 && _value is UInt64)
            {
                v.value = BitConverter.GetBytes((UInt64)_value);
            }
            else if (v.type == Type.BOOL && _value is bool)
            {
                v.value = ((bool)_value) ? new byte[1] {
                    1
                } : new byte[] { 0 };
            }
            else if (v.type == Type.Float32 && _value is float)
            {
                v.value = BitConverter.GetBytes((float)_value);
            }
            else if (v.type == Type.Float64 && _value is double)
            {
                v.value = BitConverter.GetBytes((double)_value);
            }
            else if (v.type == Type.BigNumber && _value is System.Numerics.BigInteger)
            {
                v.value = ((System.Numerics.BigInteger)_value).ToByteArray();
            }
            else if (v.type == Type.String && _value is string)
            {
                v.value = System.Text.Encoding.UTF8.GetBytes((string)_value);
            }
            else
            {
                throw new Exception("error value type:want[" + _type + "] value is[" + _value.GetType() + "]");
            }
            v.typedvalue = _value;
            return(v);
        }