栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

C#常用数据转换

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#常用数据转换

C#常用数据转换

不多说上代码,用于个人备忘,仅供参考。

		//int转byte[]
        public static byte[] IntToBytes(int val)
        {
            byte[] res = new byte[4];
            res[0] = (byte)(val >> 0);
            res[1] = (byte)(val >> 8);
            res[2] = (byte)(val >> 16);
            res[3] = (byte)(val >> 24);
            return res;
        }

        //int转byte[]
        public static byte[] IntToBytes1(int val)
        {
            return BitConverter.GetBytes(val);
        }

        //byte[]转int
        public static int BytesToInt(byte[] bytes)
        {
            int res = (bytes[0] | (bytes[1] << 8) | (bytes[2] << 16) | (bytes[3] << 24));
            return res;
        }

        //byte[]转int
        public static int BytesToInt1(byte[] bytes)
        {
            return BitConverter.ToInt32(bytes);
        }

        //字符串转byte[]
        public static byte[] StringToBytes(string str)
        {
            return Encoding.Default.GetBytes(str);
        }

        //byte[]转字符串
        public static string BytesToString(byte[] bytes)
        {
            return Encoding.Default.GetString(bytes);
        }

        //double转int —— 只取整数,不做四舍五入
        public static int DoubleToInt(double val)
        {
            return (int)val;
        }

        //double转int —— 四舍五入
        public static int DoubleToInt1(double val)
        {
            return Convert.ToInt32(val);
        }

        //int转double
        public static double IntToDouble(int val)
        {
            return Convert.ToDouble(val);
        }

        //字符串转int
        public static int StringToInt(string str)
        {
            return Convert.ToInt32(str);
        }

        //字符串转int
        public static int StringToInt1(string str)
        {
            return int.Parse(str);
        }

        //int转字符串
        public static string IntToString(int val)
        {
            return val.ToString();
        }

        //int转字符串
        public static string IntToString1(int val)
        {
            return Convert.ToString(val);
        }

        //int转字符串
        public static string IntToString2(int val)
        {
            return string.Format("{0}", val);
        }

        //struct转byte[]
        public static byte[] StructToBytes(object structObj)
        {
            int size = Marshal.SizeOf(structObj);
            IntPtr buffer = Marshal.AllocHGlobal(size);
            try
            {
                Marshal.StructureToPtr(structObj, buffer, false);
                byte[] bytes = new byte[size];
                Marshal.Copy(buffer, bytes, 0, size);
                return bytes;
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }

        //byte[]转struct
        public static object BytesToStruct(byte[] bytes, Type type)
        {
            int size = Marshal.SizeOf(type);
            IntPtr buffer = Marshal.AllocHGlobal(size);
            try
            {
                Marshal.Copy(bytes, 0, buffer, size);
                return Marshal.PtrToStructure(buffer, type);
            }
            finally
            {
                Marshal.FreeHGlobal(buffer);
            }
        }

更加详细的请自行到微软官网参考
Convert类 和 Byte.Parse 方法等

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/972601.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号