Last active
March 10, 2025 10:00
-
-
Save yuygfgg/a1cf98baf681a6195548954cca73e5b8 to your computer and use it in GitHub Desktop.
ip fomats
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <set> | |
#include <string> | |
#include <sstream> | |
#include <arpa/inet.h> | |
#include <cstdint> | |
#include <iomanip> | |
#include <regex> | |
uint32_t parseIPString(const std::string& input) { | |
if (input.find('.') == std::string::npos) { | |
if (input.substr(0, 2) == "0x" || input.substr(0, 2) == "0X") { | |
std::stringstream ss; | |
uint32_t value; | |
ss << std::hex << input.substr(2); | |
ss >> value; | |
if (!ss.fail()) { | |
return value; | |
} | |
} | |
try { | |
return std::stoul(input); | |
} catch (...) { | |
throw std::runtime_error("无效的IP地址格式"); | |
} | |
} | |
std::vector<std::string> parts; | |
std::stringstream ss(input); | |
std::string item; | |
while (std::getline(ss, item, '.')) { | |
parts.push_back(item); | |
} | |
if (parts.size() > 4 || parts.empty()) { | |
throw std::runtime_error("无效的IP地址格式"); | |
} | |
uint32_t ipInt = 0; | |
for (size_t i = 0; i < parts.size(); ++i) { | |
uint32_t partValue = 0; | |
std::string& part = parts[i]; | |
if (part.substr(0, 2) == "0x" || part.substr(0,2) == "0X") { | |
std::stringstream ss; | |
ss << std::hex << part.substr(2); | |
ss >> partValue; | |
} else if (part[0] == '0' && part.size() > 1 && part[1] != 'x' && part[1] != 'X') { | |
std::stringstream ss; | |
ss << std::oct << part.substr(1); | |
ss >> partValue; | |
} else { | |
std::stringstream ss; | |
ss << part; | |
ss >> partValue; | |
} | |
if (partValue > 0xFF) { | |
throw std::runtime_error("IP地址部分值超出范围 (0-255)"); | |
} | |
ipInt = (ipInt << 8) | (partValue & 0xFF); | |
} | |
if (parts.size() < 4) { | |
ipInt <<= (8 * (4 - parts.size())); | |
} | |
return ipInt; | |
} | |
std::string intToHex(uint32_t num) { | |
std::stringstream ss; | |
ss << "0x" << std::hex << num; | |
return ss.str(); | |
} | |
std::string intToOct(uint32_t num) { | |
std::stringstream ss; | |
ss << "0" << std::oct << num; | |
return ss.str(); | |
} | |
std::set<std::string> generateIpForms(const std::string& ipStr) { | |
std::set<std::string> result; | |
uint32_t ipInt; | |
try { | |
ipInt = parseIPString(ipStr); | |
} catch (const std::exception& e) { | |
std::cout << e.what() << std::endl; | |
return result; | |
} | |
uint32_t A = (ipInt >> 24) & 0xFF; | |
uint32_t B = (ipInt >> 16) & 0xFF; | |
uint32_t C = (ipInt >> 8) & 0xFF; | |
uint32_t D = ipInt & 0xFF; | |
std::vector<std::vector<uint32_t>> partsList = { | |
{A, B, C, D}, | |
{A, B, static_cast<uint32_t>((C << 8) + D)}, | |
{A, static_cast<uint32_t>((B << 16) + (C << 8) + D)}, | |
{ipInt} | |
}; | |
for (const auto& parts : partsList) { | |
std::vector<std::vector<std::string>> baseRepresentations; | |
for (uint32_t part : parts) { | |
std::vector<std::string> representations; | |
representations.push_back(std::to_string(part)); | |
representations.push_back(intToOct(part)); | |
representations.push_back(intToHex(part)); | |
baseRepresentations.push_back(representations); | |
} | |
std::vector<std::vector<std::string>> combinations; | |
combinations.push_back(std::vector<std::string>()); | |
for (const auto& reps : baseRepresentations) { | |
std::vector<std::vector<std::string>> newCombinations; | |
for (const auto& combo : combinations) { | |
for (const auto& rep : reps) { | |
std::vector<std::string> newCombo = combo; | |
newCombo.push_back(rep); | |
newCombinations.push_back(newCombo); | |
} | |
} | |
combinations = newCombinations; | |
} | |
for (const auto& combo : combinations) { | |
std::string ipForm; | |
for (size_t i = 0; i < combo.size(); ++i) { | |
if (i > 0) ipForm += "."; | |
ipForm += combo[i]; | |
} | |
result.insert(ipForm); | |
} | |
} | |
result.insert(std::to_string(ipInt)); | |
result.insert(intToHex(ipInt)); | |
return result; | |
} | |
int main(int argc, char* argv[]) { | |
if (argc != 2) { | |
std::cout << "用法: " << argv[0] << " <IP地址>" << std::endl; | |
std::cout << "支持的格式:" << std::endl; | |
std::cout << " - 标准格式 (如: 1.0.0.1)" << std::endl; | |
std::cout << " - 十进制整数 (如: 16777217)" << std::endl; | |
std::cout << " - 十六进制 (如: 0x1000001)" << std::endl; | |
std::cout << " - 混合格式 (如: 159.0xc70042)" << std::endl; | |
return 1; | |
} | |
std::string ipAddress = argv[1]; | |
std::set<std::string> allForms = generateIpForms(ipAddress); | |
if (!allForms.empty()) { | |
std::cout << "IP地址 " << ipAddress << " 的所有表示形式:" << std::endl; | |
for (const auto& form : allForms) { | |
std::cout << form << std::endl; | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment