예제 #1
0
        /// <summary>
        /// Преобразовать массив байт в значение, тип которого задан dataType
        /// </summary>
        protected static object BytesToObj(byte[] bytes, int index, int dataType)
        {
            switch (dataType)
            {
            case DataTypes.Integer:
                return(BitConverter.ToInt32(bytes, index));

            case DataTypes.Double:
                return(BitConverter.ToDouble(bytes, index));

            case DataTypes.Boolean:
                return(bytes[index] > 0);

            case DataTypes.DateTime:
                return(Arithmetic.DecodeDateTime(BitConverter.ToDouble(bytes, index)));

            case DataTypes.String:
                int strDataSize = BitConverter.ToUInt16(bytes, index);
                index += 2;
                if (index + strDataSize > bytes.Length)
                {
                    strDataSize = bytes.Length - index;
                }
                // для данных в кодировке ASCII метод Encoding.UTF8.GetString() тоже будет корректно работать
                return(strDataSize > 0 ? Encoding.UTF8.GetString(bytes, index, strDataSize) : "");

            default:
                return(DBNull.Value);
            }
        }
예제 #2
0
        /// <summary>
        /// Преобразовать массив байт в значение, тип которого задан dataType
        /// </summary>
        protected static object BytesToObj(byte[] bytes, int index, int dataType)
        {
            switch (dataType)
            {
            case DataTypes.Integer:
                return(BitConverter.ToInt32(bytes, index));

            case DataTypes.Double:
                return(BitConverter.ToDouble(bytes, index));

            case DataTypes.Boolean:
                return(bytes[index] > 0);

            case DataTypes.DateTime:
                return(Arithmetic.DecodeDateTime(BitConverter.ToDouble(bytes, index)));

            case DataTypes.String:
                int len = BitConverter.ToUInt16(bytes, index);
                index += 2;
                if (index + len > bytes.Length)
                {
                    len = bytes.Length - index;
                }
                return(len > 0 ? Encoding.Default.GetString(bytes, index, len) : "");

            default:
                return(DBNull.Value);
            }
        }