Skip to content

Instantly share code, notes, and snippets.

@paigeadelethompson
Last active August 16, 2024 11:44
Show Gist options
  • Save paigeadelethompson/037639bb04468ad0ea3da0ca72aa45a3 to your computer and use it in GitHub Desktop.
Save paigeadelethompson/037639bb04468ad0ea3da0ca72aa45a3 to your computer and use it in GitHub Desktop.
void print_ip(unsigned int ip)
{
unsigned char bytes[4];
bytes[0] = ip & 0xFF;
bytes[1] = (ip >> 8) & 0xFF;
bytes[2] = (ip >> 16) & 0xFF;
bytes[3] = (ip >> 24) & 0xFF;
printf("%d.%d.%d.%d\n", bytes[3], bytes[2], bytes[1], bytes[0]);
}
uint32_t getDecimalValueOfIPV4_String(const char* ipAddress)
{
uint8_t ipbytes[4]={};
int i =0;
int8_t j=3;
while (ipAddress+i && i<strlen(ipAddress))
{
char digit = ipAddress[i];
if (isdigit(digit) == 0 && digit!='.'){
return 0;
}
j=digit=='.'?j-1:j;
ipbytes[j]= ipbytes[j]*10 + atoi(&digit);
i++;
}
uint32_t a = ipbytes[0];
uint32_t b = ( uint32_t)ipbytes[1] << 8;
uint32_t c = ( uint32_t)ipbytes[2] << 16;
uint32_t d = ( uint32_t)ipbytes[3] << 24;
return a+b+c+d;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment