2

32位数从主机字节顺序转换成网络字节顺序

 3 years ago
source link: https://www.lpime.cn/article/113
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
32位数从主机字节顺序转换成网络字节顺序 - Ryan_naiquan
#include <iostream>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")

using namespace std;

//unsigned long long htonll(unsigned long long val)
//{
//	return (((unsigned long long)htonl((int)((val<<32)>>32)))<<32) | (unsigned int)htonl((int)(val>>32));
//}

template<typename T>
auto Swap(T val) -> decltype(val)
{
	if (std::is_same<T, unsigned short>::value)
	{
		return htons(val);
	}
	else if (std::is_same<T, int32_t>::value)
	{
		return htonl(val);
	}
	return val;
}

int main()
{
	int32_t aa = 27;
	aa = Swap(aa);


	std::cout << aa << std::endl;

	system("PAUSE");
	return 0;
}

本文由 Ryan 创作,采用 知识共享署名4.0 国际许可协议进行许可
本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名
最后编辑时间为: 2021/05/18 10:07


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK