QT快速使用ntohs,需要注意3点:
1. ntohs只是转换相邻的2个字节
2. 引入头文件#include <windows.h>
3. 需要加上win32{LIBS+=-lws2_32} 下面是一个简单的小例子:
#include <QtGui/QApplication>
#include <QDebug>
#include <windows.h> int main(int argc, char *argv[])
{
QApplication a(argc, argv); qDebug("unsigned int: %d", sizeof(unsigned int));
qDebug("unsigned int: %d", sizeof(unsigned short)); unsigned int uia = ;
unsigned short usa = ;
unsigned int uib = ;
unsigned short usb = ; qDebug("00000000 00000000 00000100 00000000(1024)-->(4)00000000 00000000 00000000 00000100: %d",ntohs(uib));
qDebug("00000000 00000000 00000000 00000100(4)-->(1024)00000000 00000000 00000100 00000000: %d",ntohs(uia)); qDebug("00000100 00000000(1024)-->(4)00000000 00000100: %d",ntohs(usb));
qDebug("00000000 00000100(4)-->(1024)00000100 00000000: %d",ntohs(usa)); return a.exec();
}

QT快速使用ntohs-LMLPHP

可以看出,尽管int是32位的,但实际上只是2个字节之间的交换。
05-02 16:04