0

I put an IP address from frame to struct:

unsigned char destination_address[4];

In my main program I load a struct:

struct ipv4 naglowek_ipv4;
upakuj_ipv4(bufor_eth_ipv4, &naglowek_ipv4);

And try show this in a "human-readable format":

printf("Destination Adress: %ld.%ld.%ld.%ld\n",
    strtol(naglowek_ipv4.destination_address[0],NULL,16),
    strtol(naglowek_ipv4.destination_address[1],NULL,16),
    strtol(naglowek_ipv4.destination_address[2],NULL,16),
    strtol(naglowek_ipv4.destination_address[3]));

This doesn't display the way I think it should. Does anyone have any idea why?

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
  • (Anyone more familiar with what "I put an IP address from frame to struct" and other oddities in here mean, please feel free to clarify in an edit, thx) –  Apr 14 '16 at 19:47

1 Answers1

1

The destination_address is not a string, it's just array of four bytes. So simplify your call to:

printf("Destination Adress: %d.%d.%d.%d\n",
        naglowek_ipv4.destination_address[0],
        naglowek_ipv4.destination_address[1],
        naglowek_ipv4.destination_address[2],
        naglowek_ipv4.destination_address[3]);

You would notice if you included the declaration of strtol (and also the fact you don't pass enough parameters to the last invocation):

#include <stdlib.h> /* provides strtol() function */
Zbynek Vyskovsky - kvr000
  • 18,186
  • 3
  • 35
  • 43
  • You have right. I too much complicated. Cen you tell me so, how I can change a unsigned short to decimal? The same example but char change to short: unsigned short life_time:8; – DevAxeQuestion Apr 14 '16 at 20:19
  • @AxelGocan : `printf("%u", (unsigned)life_time)` or to buffer use `char buf[16]; sprintf(buf, "%u", (unsigned)life_time); – Zbynek Vyskovsky - kvr000 Apr 15 '16 at 06:10