아래 표시합니다. 예를 들어 numA에 256을 3 배 곱한 경우 숫자는 음수가되지만 256을 두 번 곱한 경우에는 음수가됩니다. 이 프로젝트의 목적은 IP 주소를 부호없는 long으로 변경 한 다음 부호없는 long을 IP 주소로 변경하는 것입니다.numA-D를 여러 번 곱하면 숫자가 갑자기 음수가 될 때 C++에서 음수를 반환하는 부호없는 long을
using namespace std;
unsigned long ip2long (string ipv4)
{
// variable to return
unsigned long rtn;
string A,B,C,D;
string delimiter = ".";
size_t position;
/* must parse string by '.' character
and then assign (A,B,C,D) to the values
and return the unsigned long last, you don't have to make
the variables unsigned longs */
int locOfA = ipv4.find('.');
int locOfB = ipv4.find('.', locOfA + 1);
int locOfC = ipv4.find('.', locOfB + 1);
int locOfD = ipv4.find('.', locOfC + 1);
A = ipv4.substr(0,locOfA);
B = ipv4.substr(locOfA + 1, locOfB - locOfA - 1);
C = ipv4.substr(locOfB + 1, locOfC - locOfB -1);
D = ipv4.substr(locOfC + 1, locOfD - locOfC -1);
int numA = atoi(A.c_str());
int numB = atoi(B.c_str());
int numC = atoi(C.c_str());
int numD = atoi(D.c_str());
cout << endl;
cout << numA << endl;
cout << numB << endl;
cout << numC << endl;
cout << numD << endl;
cout << endl;
// assigning a unsigned long to the sum of the algorithm
cout << (numA * 256 * 256) + << endl;
cout << (256 * 256 * 256 * numB) << endl;
cout << (256 * numC) << endl;
cout << (256 * numD) << endl;
rtn = numD + (256 * numC) + (256 * 256 * numB) + (256 * 256 * 256 * numA);
return rtn;
}
따라서 numA, numB, numC 및 numD 변수를 *** signed *** int로 선언하지 마십시오. *** 부호없는 *** 키워드를 명시 적으로 사용하지 않으면 변수에 서명됩니다. – enhzflep
[부호가있는 정수 오버플로] (http : //en.wikipedia.org/wiki/Integer_overflow)는 정의되지 않은 동작입니다. –