Originally Posted by
bobv
Any example? When you know how everything seems easy, but if you don't well... I know it's a u32 and what the values are but getting it there is another story, at leaset for me. The fact that the 4 bytes need to be in little endian format is another matter but I can deal w/that.
Code:
// IP converter
#include <stdio.h>
#include <string.h>
unsigned long ConvertIP(char *IPString)
{
union t_cvt
{ unsigned long ip;
unsigned char bv[4]; }
cvt;
sscanf(IPString,"%hhu.%hhu.%hhu.%hhu",&cvt.bv[0],&cvt.bv[1],&cvt.bv[2],&cvt.bv[3]);
// for diagnostic only remove in use
printf("%u %u %u %u\n\n",cvt.bv[0],cvt.bv[1],cvt.bv[2],cvt.bv[3]);
return cvt.ip; }
int main (void)
{ char ipstr[17];
unsigned int ipaddr;
printf("Enter an IP address : ");
scanf("%s",ipstr);
ipaddr = ConvertIP(ipstr);
printf("\n %x\n\n",ipaddr);
return 0; }
To flip between little endian and big endian... simply reverse the assignment order in line 12