c++ Convert struct to bytes
程序员文章站
2022-04-20 19:16:22
D:\stock\Tskingfromgoogle\src\NetTS\TW.cpp Convert struct to bytes ......
d:\stock\tskingfromgoogle\src\netts\tw.cpp
convert struct to bytes
//convert struct to bytes 2019/05/20 by freeman
int constructloginbuffer(byte* buffer, size_t maxlen, lpctstr lpszuser, lpctstr lpszpasswd)
{
int nlen = 0;
int nlenuser = strlen(lpszuser);
int nlenpasswd = strlen(lpszpasswd);
tw_login packet;
memset(&packet,0,sizeof(packet));
packet.m_header.m_magic = tw_magic;
if(maxlen < (int)(5 + sizeof(tw_header) + nlenuser + nlenpasswd))
{
nlen = 5 + sizeof(tw_header);
return -1;
}
packet.m_name_len = (word)nlenuser;
memcpy(packet.m_data, lpszuser, nlenuser);
*(word*)(packet.m_data + nlenuser) = (word)nlenpasswd;
memcpy(packet.m_data + nlenuser + sizeof(word), lpszpasswd, nlenpasswd);
nlen = 5 + sizeof(tw_header) + nlenuser + nlenpasswd;
constructlength(packet.m_header, nlen - sizeof(packet.m_header));
if(buffer)
memcpy(buffer, &packet, nlen);
return nlen;
}
推荐阅读
-
C# dictionary to bytes and bytes convert to dictionary
-
c++ Convert struct to bytes
-
C++基础 class、struct、union详细
-
从结构struct谈到类class(基于C++实现)
-
Convert a Number to Hexadecimal(C++数字转换为十六进制数)
-
深入了解C++ 结构体(struct)与共用体(union)
-
C++之结构体struct
-
C++中struct和class的区别介绍
-
C/C++ Data alignment 及 struct size深入分析
-
C++ struct class异同