zoukankan      html  css  js  c++  java
  • 常用转换函数

    一、Unicode和UTF8互相转化

    // 转换接收到的字符串
    public string UTF8ToUnicode(string recvStr)
    {
    byte[] tempStr = Encoding.UTF8.GetBytes(recvNotify);
    byte[] tempDef = Encoding.Convert(Encoding.UTF8, Encoding.Default, tempStr);
        string msgBody = Encoding.Default.GetString(tempDef);
        return msgBody;
    }
    // 转换要发送的字符数组
    public byte[] UnicodeToUTF8(string sendStr)
    {
        string tempStr = Encoding.UTF8.GetString(sendStr);
        byte[] msgBody = Encoding.UTF8.GetBytes(tempUTF8);
        return msgBody;
    }
    

    二、其他类型互相转化

     private DateTime LongToDateTime(uint times)
            {
                long _times =(long)times + 28800;
                DateTime ds = new DateTime(1970, 1, 1, 0, 0, 0,DateTimeKind.Unspecified).AddSeconds(_times);
                return ds;
            }
    
            private byte [] StructToBytes(Object structobj , int size)
            {
                byte []tempBytes = new byte [size];
                IntPtr strcutIntptr = Marshal.AllocHGlobal(size);
                //将结构体拷贝到内存中
                Marshal.StructureToPtr(structobj, strcutIntptr, false);
                //从内存空间拷贝到byte数组中
                Marshal.Copy(strcutIntptr, tempBytes, 0, size);
                Marshal.FreeHGlobal(strcutIntptr);
                return tempBytes;
    
            }
    
            private object BytesToStruct(byte[] bytes ,Type _type)
            {
                int _size = Marshal.SizeOf(_type);
                IntPtr structInnptr = Marshal.AllocHGlobal(_size);
                Marshal.Copy(bytes, 0, structInnptr, _size);
                object obj = Marshal.PtrToStructure(structInnptr, _type);
                Marshal.FreeHGlobal(structInnptr);
                return obj;
            }
    
            private byte [] IntptrToBytes (IntPtr tempIntptr ,int _size)
            {
                byte[] tempBytes = new byte[_size];
                //从内存空间拷贝到byte数组中
                Marshal.Copy(tempIntptr, tempBytes, 0, _size);
                return tempBytes;
            }
    
            private IntPtr BytesToInptr(byte[] bytes, Type _type)
            {
                int _size = Marshal.SizeOf(_type);
                IntPtr structInnptr = Marshal.AllocHGlobal(_size);
                Marshal.Copy(bytes, 0, structInnptr, _size);
                return structInnptr;
            }
    

    本文来自博客园,作者:農碼一生,转载请注明原文链接:https://www.cnblogs.com/wml-it/p/15763892.html


    技术的发展日新月异,随着时间推移,无法保证本博客所有内容的正确性。如有误导,请大家见谅,欢迎评论区指正!
    个人开源代码链接:
    GitHub:https://github.com/ITMingliang
    Gitee:https://gitee.com/mingliang_it
    GitLab:https://gitlab.com/ITMingliang
    进开发学习交流群:

  • 相关阅读:
    [转]好习惯养成的10个步骤
    模拟资料
    [转]暗时间
    [转]30个小改变,造就你的卓越人生
    [转]Word 2007文档中图片不显示或对象不显示的解决方法
    ubuntu 10.04 安转2.6.38内核
    [转]可以让你少奋斗10年的工作经验
    [转]Vim 复制粘帖格式错乱问题的解决办法
    C# 获取类中所有的属性
    sql 脚本
  • 原文地址:https://www.cnblogs.com/wml-it/p/15763892.html
Copyright © 2011-2022 走看看