using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace Misc
{
    public class Converter
    {
        /// <summary>
        /// 为了兼容minigui GB2312 格式的 bytes  转 字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string BytesToString(byte[] bytes)
        {
            return Encoding.GetEncoding("GB2312").GetString(bytes);//System.Text.Encoding.UTF8.GetString(bytes);
        }
        /// <summary>
        /// 为了兼容minigui GB2312 格式的 bytes  转 字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="index"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static string BytesToString(byte[] bytes, int index, int count)
        {
            return Encoding.GetEncoding("GB2312").GetString(bytes, index, count);// System.Text.Encoding.UTF8.GetString(bytes, index, count);
        }
        /// <summary>
        /// 为了兼容minigui 字符串转 GB2312 格式的 bytes 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static byte[] StringToBytes(string str)
        {
            if (string.IsNullOrEmpty(str))
                return null;
            return Encoding.GetEncoding("GB2312").GetBytes(str);// System.Text.Encoding.UTF8.GetBytes(str);
        }
        /// <summary>
        /// DateTime 转字符串，基本不用
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static string DateTimeToString(DateTime dt)
        {
            return dt.ToString();
        }
        /// <summary>
        /// 给 linux 的time_t 使用， DateTime 转 time_t
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static UInt32 DateTime2Time_T(DateTime dt)
        {
            return (UInt32)dt.Subtract(DateTime.Parse("01/01/1970")).TotalSeconds;
        }
        /// <summary>
        /// 给 linux 的time_t 使用， time_t 转 DateTime
        /// </summary>
        /// <param name="time_t"></param>
        /// <returns></returns>
        public static DateTime Time_T2DateTime(UInt32 time_t)
        {
            return DateTime.Parse("01/01/1970").AddSeconds(time_t);
        }
        /// <summary>
        /// ip port 转 bytes
        /// </summary>
        /// <param name="ep"></param>
        /// <returns></returns>
        public static byte[] IPEndPoint2Bytes(IPEndPoint ep) 
        {
            return StringToBytes(ep.ToString());
        }
        /// <summary>
        /// bytes 转 ip port 
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static IPEndPoint Bytes2IPEndPoint(byte[] bytes) 
        {
            
            string str = BytesToString(bytes);
            return StringConverter.ToIPEndPoint(str);
            
        }
        /// <summary>
        /// bytes 转 ip port
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="index"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static IPEndPoint Bytes2IPEndPoint(byte[] bytes, int index, int count)
        {

            string str = BytesToString(bytes, index, count);
            return StringConverter.ToIPEndPoint(str);

        }
        /// <summary>
        /// 根据类型 字符串 转 数据
        /// </summary>
        /// <param name="val">字符串</param>
        /// <param name="type">类型</param>
        /// <returns>数据</returns>
        public static object Parse(string val, Type type)
        {
            if (type == typeof(UInt16))
                return UInt16.Parse(val);
            if (type == typeof(UInt32))
                return UInt32.Parse(val);
            if (type == typeof(bool))
                return bool.Parse(val);
            if (type == typeof(Int16))
                return Int16.Parse(val);
            if (type == typeof(Int32))
                return Int32.Parse(val);
            if (type == typeof(float))
                return float.Parse(val);
            return null;
        }
    }
}
