[英]C++ Convert Uint32 IP address to text x.x.x.x
我想將Uint32 IP地址轉換為連接字符串。
在這個過程中我得到了uint8數據,但我需要將其更改為const char *,以便能夠將其連接到IP的其他部分,以便能夠在一個變量中打印完整的IP。
如何將uint 8更改為const char *? 或者是否有更好的方法來進行所有轉換過程?
uint32 ipAddress = GetHostIp();
if (ipAddress)
{
const int NBYTES = 4;
uint8 octet[NBYTES];
int x;
char *ipAddressFinal;
for (x = 0; x < NBYTES; x++)
{
octet[x] = (ipAddress >> (x * 8)) & (uint8)-1;
}
for (x = NBYTES - 1; x >= 0; --x)
{
if (NBYTES==4)
{
const char *IPPart = octet[x]; // HERE IS THE BUG!!!!! ?
strcpy(ipAddressFinal, IPPart);
}
else
{
const char *IPPart = octet[x]; // HERE IS THE BUG!!!!! ?
strcat(ipAddressFinal, IPPart);
}
if (x > 0)
strcat(ipAddressFinal, ".");
}
LogAlways("IP: %s", ipAddressFinal);
}
編輯
謝謝你們 - 問題解決了! 謝謝大家! 在短暫的等待時間內獲得非常好的答案真是太棒了! 特別感謝Lacrymology! 這里現在是工作代碼,我不使用Linux我應該寫下我的操作系統等...
if (ipAddress)
{
const int NBYTES = 4;
uint8 octet[NBYTES];
char ipAddressFinal[16];
for(int i = 0 ; i < NBYTES ; i++)
{
octet[i] = ipAddress >> (i * 8);
}
sprintf(ipAddressFinal, "%d.%d.%d.%d", octet[3], octet[2], octet[1], octet[0]);
LogAlways("IP: \"%s\"", ipAddressFinal);
}
猜測你正在使用Linux - gethostip()似乎出現在Linux手冊頁中。 無論如何,如果是這樣的話,那么使用inet_ntoa()
呢?
sprintf(ip_src, "%s", inet_ntoa(ipdata->ip_src));
當然,假設char* ip_src
有足夠的空間來容納ip地址。 旨在將struct in_addr
轉換為char*
。
包括: #include <arpa/inet.h>
怎么樣
uint32 ipAddress = GetHostIp();
if (ipAddress) {
char ipAddr[16];
snprintf(ipAddr,sizeof ipAddr,"%u.%u.%u.%u" ,(ipAddress & 0xff000000) >> 24
,(ipAddress & 0x00ff0000) >> 16
,(ipAddress & 0x0000ff00) >> 8
,(ipAddress & 0x000000ff));
// depending on the byte order your GetHostIp() returns the IP address in
// you might need to reverse the above (i.e. print (ipAddress &0x000000ff)) first.
LogAlways("IP: %s", ipAddr);
}
您也可以使用inet_ntoa或getnameinfo將IP地址轉換為字符串。
首先,你標記為“HERE is the BUG”的行應該像sprintf(“%d”,octet [x]); 但我會告訴你我認為更好的解決方案(絕不是最好的)
uint32 ipAddress = GetHostIp();
if (ipAddress)
{
const int NBYTES = 4;
uint8 octet[NBYTES];
int x;
char *ipAddressFinal[16];
for(int i = 0 ; i < NBYTES ; i++)
{
octet[i] = ipAddress >> (i * 8);
}
sprintf("%d.%d.%d.%d", octet[0], octet[1], octet[2], octet[3]);
}
現在,這是“錯誤的”,因為我假設IP地址中有4個字節,但它完成了這項工作。 同樣假設,您可以更改for循環
if (ipAddress)
{
union {
uint32 raw;
uint8 octet[4];
} ip;
ip.raw = ipAddress;
sprintf("%d.%d.%d.%d", ipAddressFinal, ip.octet[0], ip.octet[1],
ip.octet[2], ip.octet[3]);
}
如果你不想在那里假設4那么你必須堅持第一種方式並做類似的事情
sprintf("%s.%d", ipAddressFinal, , ipAddressFinal, ip.octet[i]);
我不知道是否有效,因為它具有相同的字符串輸入和輸出
為什么不只是inet_ntoa(*(struct in_addr *)&uint32var)
? 正如這里建議的那樣https://stackoverflow.com/a/9961072/93647
這是一個實際的C ++解決方案
#include <arpa/inet.h>
std::string GetIPString(uint32_t x) const
{
char buffer[INET_ADDRSTRLEN + 1];
auto result = inet_ntop(AF_INET, &x, buffer, sizeof(buffer));
if (result == nullptr) throw std::runtime_error("Can't convert IP4 address");
return buffer;
}
謝謝你們 - 問題解決了! 謝謝大家! 在短暫的等待時間內獲得非常好的答案真是太棒了! 特別感謝Lacrymology! 這里現在是工作代碼,我不使用Linux我應該寫下我的操作系統等...
if(ipAddress){const int NBYTES = 4; uint8 octet [NBYTES]; char ipAddressFinal [15]; for(int i = 0; i <NBYTES; i ++){octet [i] = ipAddress >>(i * 8); } sprintf(ipAddressFinal,“%d。%d。%d。%d”,octet [3],octet [2],octet [1],octet [0]); LogAlways(“IP:\\”%s \\“”,ipAddressFinal); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.