c - 如何将 IPv4 地址转换为十进制
问题描述
是否有将点分十进制表示法(例如“192.168.0.1”)的 IPv4 地址转换为十进制值的功能?
有几个与此类似的问题,但都在寻找或拥有涉及编写函数来解决答案的答案。我正在寻找图书馆中的现有功能。
解决方案
It turns out that Winsock 2 provides the inet_addr()
function, which simply returns the equivalent decimal value for the address.
#include <stdio.h>
#include <winsock2.h>
int main(void)
{
char* pAddr = "192.168.0.1";
unsigned long value = inet_addr(pAddr);
if (value == INADDR_NONE)
printf("Invalid format of IP address");
else
printf("Decimal representation of %s is: %lu", pAddr, value);
return 0;
}
推荐阅读
- c++ - RcppNumerical 给出的积分结果与 R 完全不同
- dji-sdk - 使用 RC 摇杆命令覆盖 OSDK 飞控命令
- c# - 将 JSON 骆驼案例转换为蛇案例(反之亦然)并将数值字符串化
- windows - appcmd 不全局设置网站允许的文件扩展名
- java - Docker OpenJDK-8 JavaFx
- postgresql - 插入或选择并返回密钥
- typescript - mat-tab-nav-bar 选项卡未显示
- java - MultiMap 与集合
- ios - 在 iOS 设备上测试时 Unity 游戏延迟
- c# - 在 ASP.NET 中执行多个 Informix SQL 查询