c++ - Qt ASCII转IP函数

标签 c++ qt qtcore qtnetwork

我正在尝试使用 Qt 创建一个 UDP 终端(自定义)——有谁知道是否有用于处理 IP 地址从 ASCII 到数字(十六进制)的转换的小部件或类,或者我是否必须编写那我自己?基本上是“192.168.1.1”->“0xC0A80101”。我不反对写它,只是想知道是否有人知道它是否已经存在。尝试搜索,运气不佳。谢谢大家

最佳答案

这里的关键类是QHostAddress如下:

main.cpp

#include <QHostAddress>

#include <QTextStream>
#include <QString>

int main()
{
    QTextStream standardOutput(stdout);
    // You could use this, too:
    // standardOutput.setIntegerBase(16);
    // standardOutPut.setNumberFlags(QTextStream::ShowBase);
    quint32 ipAddress = QHostAddress("192.168.1.1").toIPv4Address();
    QString hexIpAddress = QString::number(ipAddress, 16);
    QString prefixedUppercaseHexIpAddress = QString("0x%1")
                                            .arg(uppercaseHexIpAddress);
    standardOutput << prefixedUppercaseHexIpAddress;
    return 0;
}

主程序

TEMPLATE = app
TARGET = main
QT = core network
SOURCES += main.cpp

构建并运行

qmake && make && ./main

输出

0xC0A80101

关于c++ - Qt ASCII转IP函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21123440/

相关文章:

c++ - VST SDK 2.4 乐器 - processReplace 和 processEvents

c++ - 提高OpenMP/SSE并行化效果

c++ - 如何将 freetype2.6 库添加到 code::blocks

c++ - 从另一个线程唤醒 QThread::exec()

c++ - QTimer 与 QPaintEvent

c# - 使 .Net 3.5 应用程序与 .Net 4.0 应用程序对话的最稳定方式

qt - 如何在 QT 中的布局上制作子部件列表?

c++ - 将 Qt::MatchExactly 设置为不区分大小写

qt - 检测到由布局管理的项目上的 anchor 。这是未定义的行为;使用 Layout.alignment 代替

c++ - Qt qglobal.h 的 Boost 版本