2

I can make function calls and receive an array of strings that represents ipv6 adress. it looks something like this

char* buffer=resolver_getstring(config, INI_BOOT_MESHINTFIPADDRESS);

if i printed buffer i will gate the ipv6 adress in string form:

dddd:0000:0000:0000:0000:0000:0000:cccc

however, the way how ipv6 address is represented in my project is with 16 hexadecimal number by using uint8_t datatype as follows

uint8_t ipadress[16]

now my problem is how can i cast (or copy the memory of buffer) to uint8_t[16]

what i would like to get is

    ipadress[0]=dd // hexadecimal number
    ipaddress[1]=dd
    ....
    ipaddress[15]=cc

is there anyway i could do ? Regards,

lferasu
  • 185
  • 2
  • 10

2 Answers2

2
#include <stdint.h>
#include <inttypes.h>
...
char *buffer="dddd:0000:0000:0000:0000:0000:0000:cccc";
uint8_t ipadress[16];
sscanf(buffer,
    "%2" SCNx8 "%2" SCNx8 ":"
    "%2" SCNx8 "%2" SCNx8 ":"
    "%2" SCNx8 "%2" SCNx8 ":"
    "%2" SCNx8 "%2" SCNx8 ":"
    "%2" SCNx8 "%2" SCNx8 ":"
    "%2" SCNx8 "%2" SCNx8 ":"
    "%2" SCNx8 "%2" SCNx8 ":"
    "%2" SCNx8 "%2" SCNx8 ,
    &ipadress[0],&ipadress[1],
    &ipadress[2],&ipadress[3],
    &ipadress[4],&ipadress[5],
    &ipadress[6],&ipadress[7],
    &ipadress[8],&ipadress[9],
    &ipadress[10],&ipadress[11],
    &ipadress[12],&ipadress[13],
    &ipadress[14],&ipadress[15]);
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • However, this methods generates error " expected ')' before 'SCNx8' in gcc compiler. it compiles and runs with out a problem in g++ any idea why and how it can be compiled with gcc? – lferasu Apr 17 '14 at 08:28
  • @lferasu `#include ` and add `-std=c99` option or `"%2" SCNx8 "%2" SCNx8 ":"` replace to `"%2hhx%2hhx:"` – BLUEPIXY Apr 17 '14 at 08:50
0

You'll need to look into strtok() (to break the string into pieces at the colon char), strtol() (to convert the pieces from hexadecimal form into binary) and some bit shifting and shuffling (to break the resulting 16 bit numbers into two separate bytes).

mfro
  • 3,286
  • 1
  • 19
  • 28
  • thats right i did split the string, i now have array of 16 strings, something like this char** tokens; *(tokens+0) ="dd" *(tokens+1)="dd" what is left is now to copy this elements of tokens to my ipadress any info about the bit shifting and shuffling would be appriciated – lferasu Apr 16 '14 at 16:18
  • did you look up strol() as suggested above? – mfro Apr 16 '14 at 16:40