Home > Net >  How to pad an IP address with leading zeroes
How to pad an IP address with leading zeroes

Time:10-08

I have seen solutions written online in C, but I want a C way to pad an IPv4 address with zeroes.

C code found online

using namespace std;

#include<iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void padZeroIP(char *str)
{
    int oct1=0;
    int oct2=0;
    int oct3=0;
    int oct4=0;

    int i = 0;

    const char s[2] = ".";
    char *token;
    
    /* get the first token */
    token = strtok(str, s);

    oct1 = atoi(token);
    
    /* walk through other tokens */
    while( token != NULL ) 
    {
        token = strtok(NULL, s);
        
        if(i==0)
            oct2 = atoi(token);
        else if(i==1)
            oct3 = atoi(token);
        else if(i==2)
            oct4 = atoi(token);
        i  ;
    }

    sprintf(str,"d.d.d.d",oct1,oct2,oct3,oct4);
}

CodePudding user response:

Here is my solution using boost to separate the string based on its delimiter (period) first.

// standard
#include <vector>
#include <string>

// boost
#include <boost/tokenizer.hpp>

std::vector<std::string> delimiter_str_to_svec(std::string delimiter_str, std::string delimiter)
{
    std::vector<std::string> svec;
    typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
    boost::char_separator<char> sepa(delimiter.c_str());
    tokenizer tokens(delimiter_str, sepa);

    for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end();   tok_iter)
    {
        try
        {
            svec.push_back(*tok_iter);
        }
        catch (boost::bad_lexical_cast &)
        {
            std::cerr << "bad lexical cast; *tok_iter = " << *tok_iter << std::endl;
        }
    }

    return svec;
}

std::string pad_ip_with_zeroes(std::string ip_address)
{
    std::string ret;
    std::vector<std::string> ip_octets;

    // get octets of IP address
    ip_octets = delimiter_str_to_svec(ip_address, ".");

    // pad octets with zeroes
    for (size_t i = 0; i < ip_octets.size(); i  )
    {
        // add zeroes if there are less than 3 numbers
        if (ip_octets.at(i).length() < 3)
        {
            ip_octets.at(i).insert(0, (3 - ip_octets.at(i).length()), '0'); // insert zeroes at front of string
        }

        // add to return
        ret  = ip_octets.at(i)   ".";
    }

    // remove last period
    ret.pop_back();

    return ret;
}

Example

192.168.2.2 becomes 192.168.002.002, or 10.0.0.2 becomes 010.000.000.002.

CodePudding user response:

#include <sstream>
#include <string>
#include <iomanip>

std::string padZeroIP(const std::string &str)
{
    std::istringstream iss(str);
    std::ostringstream oss;

    std::string token;
    bool first = true;

    while (std::getline(iss, token, '.'))
    {
        if (first)
            first = false;
        else
            oss << '.';

        int oct = std::stoi(token);
        oss << std::setw(3) << std::setfill('0') << oct;
    }

    return oss.str();
}
#include <iostream>

std::cout << padZeroIP("192.168.0.1"); // prints "192.168.000.001"

Online Demo

  • Related