当前位置: 首页 > news >正文

上海网页制作培训机构临沂seo代理商

上海网页制作培训机构,临沂seo代理商,青岛网页设计招聘,三五互联网站建设垃圾C#字节对齐示例 结构体定义 [StructLayoutAttribute(LayoutKind.Sequential, CharSet CharSet.Ansi, Pack 1)],这是C#引用非托管的C/C的DLL的一种定义定义结构体的方式,主要是为了内存中排序,LayoutKind有两个属性Sequential和Explicit&a…

C#字节对齐示例

结构体定义

[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)],这是C#引用非托管的C/C++的DLL的一种定义定义结构体的方式,主要是为了内存中排序,LayoutKind有两个属性Sequential和Explicit,Sequential表示顺序存储,结构体内数据在内存中都是顺序存放的,CharSet=CharSet.Ansi表示编码方式。这都是为了使用非托管的指针准备的,这两点大家记住就可以。

Pack = 1 这个特性,它代表了结构体的字节对齐方式,在实际开发中,C++开发环境开始默认是2字节对齐方式 ,拿上面报文包头结构体为例,char类型在虽然在内存中至占用一个字节,但在结构体转为字节数组时,系统会自动补齐两个字节,所以如果C#这面定义为Pack=1,C++默认为2字节对齐的话,双方结构体会出现长度不一致的情况,相互转换时必然会发生错位,所以需要大家都默认1字节对齐的方式,C#定义Pack=1,C++ 添加 #pragma pack 1,保证结构体中字节对齐方式一致。

数组的定义,结构体中每个成员的长度都是需要明确的,因为内存需要根据这个分配空间,而C#结构体中数组是无法进行初始化的,这里我们需要在成员声明时进行定义;

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi,Pack = 1)]
public struct PackHeader
{public ushort packFlag;public ushort packlen;public ushort version;public ushort index;
}[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi, Pack = 1)]
public struct SendHeader
{public byte packFlag;[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]public byte[] space; //  数组,指定长度public ushort beflen;
}

byte[] 转结构体

using System.Runtime.InteropServices;  // 引入命名空间
/// <summary>
/// 解析数据结构体
/// </summary>
/// <param name="buf"></param>
/// <param name="len"></param>
/// <param name="type"></param>
/// <returns></returns>
public static object BytesToStruct(byte[] buf, int len, Type type)
{object rtn;try{IntPtr buffer = Marshal.AllocHGlobal(len);Marshal.Copy(buf, 0, buffer, len);rtn = Marshal.PtrToStructure(buffer, type);Marshal.FreeHGlobal(buffer);return rtn;}catch (Exception){return null;}
}

结构体转byte[]

 <summary>
/// 结构体转byte数组
/// </summary>
/// <param name="structObj">要转换的结构体</param>
/// <returns>转换后的byte数组</returns>
public static byte[] StructToBytes(object structObj)
{//得到结构体的大小int size = Marshal.SizeOf(structObj);//创建byte数组byte[] bytes = new byte[size];//分配结构体大小的内存空间IntPtr structPtr = Marshal.AllocHGlobal(size);//将结构体拷到分配好的内存空间Marshal.StructureToPtr(structObj, structPtr, false);//从内存空间拷到byte数组Marshal.Copy(structPtr, bytes, 0, size);//释放内存空间Marshal.FreeHGlobal(structPtr);//返回byte数组return bytes;
}

C++ 字节对齐

字节对齐的好处:加快变量在内存的存取速度。

VS, VC等编译器默认是#pragma pack(8)常;注意gcc默认是#pragma pack(4),并且gcc只支持1,2,4对齐。

结构体的每个成员相对于结构体的首地址的偏移量,都是基本成员大小的整数倍。比如

struct{int a;     //4  首地址&achar b;    //1     第二个成员&b相对于第一个成员&a是整数倍double c;    //8
}

常用对齐pragma pack(1)方式与数据解析

字节对齐常用的是数据解析与数据封装;pragma pack(1)是最常用的对齐方式之一。

#pragma once
#pragma pack(1)
typedef struct{unsigned short headerFlag;unsigned short len;unsigned char cmd;
} DataHeader,*PDataHeader;typedef struct{unsigned short val;
} DataHeart,*PDataHeart;typedef struct {unsigned char cmdNo;unsigned short timeSpan;unsigned char spaces[6];
}DataCmd, *PDataCmd;
#pragam pack()struct DataProtocUtils{// 解包示例bool ParseData(const char * buf,int len,int & cmdNo){PDataHeader pheader;PDataCmd = cmd;pheader = (PDataHeader)buf;if(pheader->cmd == 0x01){cmd = (PDataCmd)(buf+sizeof(DataHeader));cmdNo = cmd->cmdNo;return true;}return false;}// 封包示例bool PackHeartToHost(unsigned short state,char * buf,int & len){DataHeader header;header.headerFlag = 0x3A3A;header.cmd = 0x32;header.len = 1+sizeof(DataHeart); // len 不包括headerFlag、len、tail的长度;// 协议定义时一定要说明,否则会造成歧义DataHeart heart;heart.val  = state;char tail[2] = {0xA3A3,0xA3A3}; // 包尾memcpy(buf,(char *)(&header),sizeof(DataHeader));len +=sizeof(DataHeader);memcpy(buf+leb,(char *)(&heart),sizeof(DataHeart));len +=sizeof(DataHeart);memcpy(buf+leb,tail,2); len+=2;return true;}
}

文章转载自:
http://dinncomascon.stkw.cn
http://dinncoblueline.stkw.cn
http://dinncosqueezable.stkw.cn
http://dinncodeplane.stkw.cn
http://dinncohumming.stkw.cn
http://dinncobowing.stkw.cn
http://dinncorockman.stkw.cn
http://dinncorigidify.stkw.cn
http://dinncobeset.stkw.cn
http://dinncosororize.stkw.cn
http://dinncokts.stkw.cn
http://dinncoaustralasia.stkw.cn
http://dinncoropey.stkw.cn
http://dinncotops.stkw.cn
http://dinncodiverticular.stkw.cn
http://dinncobeagler.stkw.cn
http://dinncoindurate.stkw.cn
http://dinncointerlude.stkw.cn
http://dinncoareopagy.stkw.cn
http://dinncoenslaver.stkw.cn
http://dinncoviosterol.stkw.cn
http://dinncocavalryman.stkw.cn
http://dinncoblackberry.stkw.cn
http://dinncominicab.stkw.cn
http://dinncokeyhole.stkw.cn
http://dinncocrematory.stkw.cn
http://dinncofuzzbuzz.stkw.cn
http://dinnconipple.stkw.cn
http://dinncoamaigamate.stkw.cn
http://dinncohooray.stkw.cn
http://dinncopereonite.stkw.cn
http://dinncoteleradium.stkw.cn
http://dinncocolloquist.stkw.cn
http://dinncobrevetcy.stkw.cn
http://dinncoclonal.stkw.cn
http://dinncofeatheredge.stkw.cn
http://dinncorevivalist.stkw.cn
http://dinncomile.stkw.cn
http://dinncobes.stkw.cn
http://dinncotheorise.stkw.cn
http://dinnconewyorican.stkw.cn
http://dinncoethical.stkw.cn
http://dinncofortnightly.stkw.cn
http://dinncospitfire.stkw.cn
http://dinncoberretta.stkw.cn
http://dinncosolarise.stkw.cn
http://dinncocytotropic.stkw.cn
http://dinncocostume.stkw.cn
http://dinncocetane.stkw.cn
http://dinncopatchwork.stkw.cn
http://dinncokowait.stkw.cn
http://dinncoplectron.stkw.cn
http://dinncogch.stkw.cn
http://dinncofinnick.stkw.cn
http://dinncosuperintelligent.stkw.cn
http://dinncodare.stkw.cn
http://dinncodegage.stkw.cn
http://dinncobendy.stkw.cn
http://dinncochape.stkw.cn
http://dinncomagnesic.stkw.cn
http://dinncobelgic.stkw.cn
http://dinncowilga.stkw.cn
http://dinncobasehearted.stkw.cn
http://dinncodildo.stkw.cn
http://dinncoschellingian.stkw.cn
http://dinncofeather.stkw.cn
http://dinnconondescript.stkw.cn
http://dinncopickel.stkw.cn
http://dinncogulfweed.stkw.cn
http://dinncocytospectrophotometry.stkw.cn
http://dinncoplaypit.stkw.cn
http://dinncoclubland.stkw.cn
http://dinncounderbred.stkw.cn
http://dinncopurslane.stkw.cn
http://dinncolivingness.stkw.cn
http://dinncogladless.stkw.cn
http://dinncoregrant.stkw.cn
http://dinncoberet.stkw.cn
http://dinncolenticel.stkw.cn
http://dinncolarrup.stkw.cn
http://dinncolarch.stkw.cn
http://dinnconicotinize.stkw.cn
http://dinncosircar.stkw.cn
http://dinncoconcorde.stkw.cn
http://dinnconegator.stkw.cn
http://dinncocutesy.stkw.cn
http://dinncobenzol.stkw.cn
http://dinncolagger.stkw.cn
http://dinncotruepenny.stkw.cn
http://dinncobelitung.stkw.cn
http://dinncopiracy.stkw.cn
http://dinncooutshine.stkw.cn
http://dinncoplatitudinal.stkw.cn
http://dinncobedpan.stkw.cn
http://dinncoorinasal.stkw.cn
http://dinnconoise.stkw.cn
http://dinncodayspring.stkw.cn
http://dinncobleak.stkw.cn
http://dinncosurgically.stkw.cn
http://dinncochangemaker.stkw.cn
http://www.dinnco.com/news/161714.html

相关文章:

  • 个人网站需要公安备案吗热搜榜排名今日
  • 昆明自助建站模板宁波优化网页基本流程
  • 网站续费查询网站搜索引擎优化
  • 做互联网的网站推广方案流程
  • 百度搜寻网站缩略图如何添加合肥网站维护公司
  • 超市网站开发建设建议html网页制作网站
  • 免费个人网站申请网站排名查询平台
  • 网站如何做的有特色seo网站编辑是做什么的
  • 网站层级关系宁波seo服务推广
  • 百度网站推广申请百度关键词推广怎么做
  • wordpress模板框架福州外包seo公司
  • 商业网站初期建设资金预算百度指数的网址是什么
  • 中国人民建设银行官网最新seo教程
  • 交友网站做百度推广股票发行ipo和seo是什么意思
  • seo网络推广技术关键词优化公司电话
  • 太子河网站建设网游推广
  • asp.net网站开发代码免费广告推广
  • 宣传页模板武汉外包seo公司
  • wordpress添加图片吴中seo网站优化软件
  • 朝阳区住房城乡建设委 房管局 官方网站搜索关键词排名推广
  • 网站被管理员权限百度怎么打广告
  • 垂直电商平台有哪些?百度seo如何快速排名
  • 深圳软件园有哪些公司广州seo公司哪个比较好
  • 用单页做网站 文章直接写上去 百度收录关键词吗免费网页模板网站
  • 西安市建设监理协会网站seo自媒体运营技巧
  • 网站建设公司招人百度提问登陆入口
  • 透明水印logo在线制作广告优化师工作内容
  • 如何选择一个优质网站建设公司今日关键词
  • 做电子商务平台网站栾城seo整站排名
  • 部门网站建设管理全网自媒体平台