coko
asked on
subnet calculator
I am trying to write a function that accepts two strings,
an ip address and a netmask, and returns another string,
which is the subnet calculated from the two input parameters.
first i convert the two strings to ip addresses
using inet_addr(ip) and inet_addr(subnet)
then i use the & operation to get the netmask in
an unsigned int format, but I don't know how to
convert that back to a string.
Is it possible to do that conversion, or am I completely
off topic here ???
Does anybody know of any code that does this successfully ?
Thanx,
corneliu
an ip address and a netmask, and returns another string,
which is the subnet calculated from the two input parameters.
first i convert the two strings to ip addresses
using inet_addr(ip) and inet_addr(subnet)
then i use the & operation to get the netmask in
an unsigned int format, but I don't know how to
convert that back to a string.
Is it possible to do that conversion, or am I completely
off topic here ???
Does anybody know of any code that does this successfully ?
Thanx,
corneliu
So what you need to know then is how to convert an unsigned int into a string?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
unsigned long int x=123456;
char *y,c;
char string[16], s[3];
int i,n;
void main()
{
y = (char *)&x;
for(i=3;i>=0;i--) {
c=*(y+i);
n=(int)c;
if (n<0) {n=n+256;}
itoa(n,s,10);
strcat(string,s);
if(i>0) strcat(string,"."); }
printf("%s\n",string);
}
#include <stdio.h>
#include <string.h>
unsigned long int x=123456;
char *y,c;
char string[16], s[3];
int i,n;
void main()
{
y = (char *)&x;
for(i=3;i>=0;i--) {
c=*(y+i);
n=(int)c;
if (n<0) {n=n+256;}
itoa(n,s,10);
strcat(string,s);
if(i>0) strcat(string,"."); }
printf("%s\n",string);
}
ASKER
no, i need to know how to convert a subnet address
like 47.245.34 from unsigned it (which is represented
something like 3462356326) to the actual address in
a str
like 47.245.34 from unsigned it (which is represented
something like 3462356326) to the actual address in
a str
I know...that is what my program does (attempts to do).
Your progrem depends on the 'endian'ness of the integer representation.
(although, if you converted from a string the same way in the first place, and don't really care about the int value, then it may not matter)
atoi is not part of ANSI C, if your library doesn't have it, you can use sprintf instead
(although, if you converted from a string the same way in the first place, and don't really care about the int value, then it may not matter)
atoi is not part of ANSI C, if your library doesn't have it, you can use sprintf instead
My code snippet assumes little endianness by the way...
BTW: atoi _is_ part of ANSI C.
Sorry, i think you must have meant itoa - so my comment is pretty
unnecessary :)
unnecessary :)
BTW again: inet_ntoa converts an 'in_addr' back to a dotted
string - i think this is what you need, coko?
string - i think this is what you need, coko?
You're right snoegler, atoi is part of ANSI C, I ment to say that itoa is not :)
nor is inet_ntoa part of ANSI C, but it is likely to be found in the same places as inet_addr
does
printf("%s\n",inet_ntoa(in et_makeadd r(inet_net work("47.2 45.34.0")> >8,inet_ad dr("47.245 .34.0")))) ;
do the job?
nor is inet_ntoa part of ANSI C, but it is likely to be found in the same places as inet_addr
does
printf("%s\n",inet_ntoa(in
do the job?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.