Skip to content

Instantly share code, notes, and snippets.

@thriqon
Created March 21, 2012 09:28
Show Gist options
  • Save thriqon/2145794 to your computer and use it in GitHub Desktop.
Save thriqon/2145794 to your computer and use it in GitHub Desktop.
IP-Adresse einlesen und als Hex ausgeben
#include <iostream>
#include <stdlib.h>
int main()
{
std::string a_ip;
std::cin >> a_ip;
int icomponents[4];
int prepos = 0;
int curpos = a_ip.find('.', prepos);
std::string tmp;
tmp = a_ip.substr(prepos, curpos - prepos);
icomponents[0] = atoi(tmp.c_str());
prepos = curpos + 1;
curpos = a_ip.find('.', prepos);
tmp = a_ip.substr(prepos, curpos - prepos);
icomponents[1] = atoi(tmp.c_str());
prepos = curpos + 1;
curpos = a_ip.find('.', prepos);
tmp = a_ip.substr(prepos, curpos - prepos);
icomponents[2] = atoi(tmp.c_str());
prepos = curpos + 1;
tmp = a_ip.substr(prepos);
icomponents[3] = atoi(tmp.c_str());
std::cout << std::hex;
for (int i=0; i < 4; i++)
{
std::cout << icomponents[i];
if (i != 3)
std::cout << '.';
}
std::cout << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment