Math problem, I am stumped.
Ok, I am not a programmer and only write small EXE programs to take care of easy tasks. I threw a program together real quick to do some simple math functions to an IP address. The point of this is to take the IP and convert it to whats called a DWORD. I won't go in depth of why I am doing this, but I am having trouble. The math doesnt seem to be doing what its supposed to. I am coming up with some crazy answers and I am not sure why. Someone please help me out.
Heres what it should do...
First Octet * 16,777,216
Second Octet * 65,536
Third Octet * 11,010,048
Forth Octet * 1
Everything compiles fine and it works, just the wrong ending value. Just as a reference 192.168.1.1 should equal 3,232,235,777. Thanks in advance.
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int seed1, seed2, seed3, seed4, total;
cout << "
####################################
# IP to 32-Bit DWORD #
# Made by Brandon Dixon #
# #
# 1.) Enter IP as Asked #
# 2.) DWORD is Calculated #
# 3.) Enjoy #
####################################";
cout << "\n\nEnter First Octet of IP : ";
cin >> seed1;
cout << "\n\nEnter Second Octet of IP : ";
cin >> seed2;
cout << "\n\nEnter Third Octet of IP : ";
cin >> seed3;
cout << "\n\nEnter Forth Octet of IP : ";
cin >> seed4;
seed1 * 16777216;
seed2 * 65536;
seed3 * 256;
total = seed1C + seed2C + seed3C + seed4;
cout << "\n\nYour 32-Bit DWORD is " << total << endl;
cout << "\n\nThank You for using the 32-Bit DWORD Convertor!\n\n";
system("pause");
return 0;
}
# 2 Re: Math problem, I am stumped.
a dword is old school speak for a 32 bit integer, which is 4 bytes, and will hold an older style IP but NOT the newer ones which have more bytes.
a fast way to do this is
union both
{
unsigned char IP[4];
unsigned int DwordData; //dword can be a keyword and you need a 32 bit int here for your platform.
}
then you just put the ip into your chars
both Converter;
Converter.IP[0] = 192;
Converter.IP[1] = 168;
Converter.IP[2] = 0;
Converter.IP[3] = 1;
and now the dword is already there:
Converter.DwordData; //this is now correct, barring endian issues -- you *might* want to byte swap it for some platforms' because some platforms store the thing in reverse. You can do this with the macro "htonl" defined on most systems, or load the IP part of the union in reverse if you prefer that.
jonnin at 2007-11-11 21:00:28 >

# 4 Re: Math problem, I am stumped.
I think he want it to be : "using the 32-Bit DWORD" ..
well as jonnin say the "int" range can't support this "double" number so unsigned int or double may help u , also to display it with showpoint and have best results u may use the header iomanip :
cout << "\n\nYour 32-Bit DWORD is "
<<setiosflags(ios::fixed)
<<setiosflags(ios::showpoint)
<<setprecision(0)
<< total << endl;
and the corrections that viorel made was coreect but shifting work only with int type .
Amahdy at 2007-11-11 21:02:26 >
