i've some troubels here, i want to convert this char array
char IP[]="2001:2AB1:30A1:2000:1000:ABC1"
to 4 int key1,key2,key3,key4 with
key1=2001
key2=2AB1
key3=30A1
key4=2000:1000:ABC1
im working in C language.
Thank you
i've some troubels here, i want to convert this char array
char IP[]="2001:2AB1:30A1:2000:1000:ABC1"
to 4 int key1,key2,key3,key4 with
key1=2001
key2=2AB1
key3=30A1
key4=2000:1000:ABC1
im working in C language.
Thank you
It looks like you want to convert an IPv6 address to four integers. I'd recommend you leverage existing library functions to accomplish this, if they're available in your environment. Specifically, inet_ntop
can convert your string to an struct in6_addr
which should be much easier to deal with.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
char *strdelch(char *str, char ch){
char *from, *to;
if(NULL==str)return str;
from=to=str;
while(*from){
if(*from != ch)
*to++ = *from;
++from;
}
*to = '\0';
return str;
}
int main(void){
char IP[]="2001:2AB1:30A1:2000:1000:ABC1";
int key1,key2,key3;
int64_t key4;
char *p = IP;
key1 = strtol(p, &p, 16);
key2 = strtol(++p, &p, 16);
key3 = strtol(++p, &p, 16);
key4 = strtoll(strdelch(++p, ':'), NULL, 16);
printf("%04X\n", key1);
printf("%04X\n", key2);
printf("%04X\n", key3);
printf("%012I64X\n", key4);
return 0;
}