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 方法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值