QString类型的IP与Uint之间互转

小端模式下:
IP转Uint

quint32 IPV4StringToInteger(const QString& ip){
    QStringList ips = ip.split(".");
    if(ips.size() == 4){
        return ips.at(3).toInt()
                | ips.at(2).toInt() << 8
                | ips.at(1).toInt() << 16
                | ips.at(0).toInt() << 24;
    }
    return 0;
}

Uint转IP

QString ipcount1 = QString::number((Uint_ip & 0xff000000));
QString ipcount2 = QString::number((Uint_ip & 0x00ff0000) >> 8);
QString ipcount3 = QString::number((Uint_ip & 0x0000ff00) >> 16);
QString ipcount4 = QString::number((Uint_ip & 0x000000ff) >> 24);
QString IP_addr = ipcount1 + "." + ipcount2 + "." + ipcount3 + "." + ipcount4);
``` // ipdb.cpp #include "ipdb.h" #include "ipdb_settings.h" #include "tracing_utils.h" #include <direct.h> #include <QDebug> IPDB::IPDB() { // 输出当前工作目录 qDebug() << "[IPDB]" << "Now working in: " << _getcwd(NULL, 0); // 初始化 MMDB 数据库操作对象 int openDatabaseStatus; openDatabaseStatus = MMDB_open(GEOIP2_CITY_MMDB, MMDB_MODE_MMAP, &CityDB); if (openDatabaseStatus != MMDB_SUCCESS) { // Open failed qCritical() << "[IPDB]" << QString("Failed to open GeoIP2 City database from %1 with error: %2").arg(GEOIP2_CITY_MMDB, MMDB_strerror(openDatabaseStatus)); } openDatabaseStatus = MMDB_open(GEOIP2_ISP_MMDB, MMDB_MODE_MMAP, &ISPDB); if (openDatabaseStatus != MMDB_SUCCESS) { // Open failed qCritical() << "[IPDB]" << QString("Failed to open GeoIP2 ISP database from %1 with error: %2").arg(GEOIP2_ISP_MMDB, MMDB_strerror(openDatabaseStatus)); } } IPDB::~IPDB() { // 关闭 MMDB MMDB_close(&CityDB); MMDB_close(&ISPDB); } // 工具函数:复制指定长度的字符串 char * IPDB::strndup(const char *str, size_t n) { size_t len; char * copy; len = strnlen(str, n); if ((copy = (char*)malloc(len + 1)) == NULL) return (NULL); memcpy(copy, str, len); copy[len] = '\0'; return (copy); } // 成员函数:在 MMDB 中查询 IP 对应的城市信息 bool IPDB::LookUpIPCityInfo( const sockaddr * ip_address, QString & cityName, QString & countryName, double & latitude, double & longitude, uint16_t & accuracyRadius, bool & isLocationValid ) { // 原函数内容保持不变 } // 成员函数:在 MMDB 中查询 IP 对应的ISP信息 bool IPDB::LookUpIPISPInfo( const sockaddr * ip_address, QString & isp, QString & org, uint & asn, QString & asOrg ) { // 原函数内容保持不变 } // 成员函数:在 MMDB 中查询 IP 对应的经纬度信息 bool IPDB::LookUpIPLocationInfo( const sockaddr * ip_address, double & latitude, double & longitude, bool & isLocationValid ) { int mmdbStatus; MMDB_lookup_result_s city_result = MMDB_lookup_sockaddr(&CityDB, ip_address, &mmdbStatus); if (mmdbStatus != MMDB_SUCCESS) { qWarning() << "[IPDB]" << "Failed to search from City database with error: " << MMDB_strerror(mmdbStatus); return false; } // 先认为经纬度信息是有效的 isLocationValid = true; // 纬度 MMDB_entry_data_s cityEntryData_latitude; int getEntryDataStatus = MMDB_get_value(&city_result.entry, &cityEntryData_latitude, "location", "latitude", NULL); if (getEntryDataStatus != MMDB_SUCCESS) { // 还是失败了 qWarning() << "[IPDB]" << "Failed to retrieve latitude data with error: " << MMDB_strerror(getEntryDataStatus); latitude = 0.0; // 但其实是无效的 isLocationValid = false; } else { qDebug() << "[IPDB]" << "Get latitude successfully: " << cityEntryData_latitude.double_value; latitude = cityEntryData_latitude.double_value; } // 经度 MMDB_entry_data_s cityEntryData_longitude; getEntryDataStatus = MMDB_get_value(&city_result.entry, &cityEntryData_longitude, "location", "longitude", NULL); if (getEntryDataStatus != MMDB_SUCCESS) { // 还是失败了 qWarning() << "[IPDB]" << "Failed to retrieve longitude data with error: " << MMDB_strerror(getEntryDataStatus); longitude = 0.0; // 但其实是无效的 isLocationValid = false; } else { qDebug() << "[IPDB]" << "Get longitude successfully: " << cityEntryData_longitude.double_value; longitude = cityEntryData_longitude.double_value; } return true; }```修改代码,使其设计面出现IP地址时延还有经纬度
03-27
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Acnidouwo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值