Find the answer to your Linux question:
Results 1 to 7 of 7
Hi, I would like to know a way by which I can store the host address in char array. For example, //packet contains eth_header, ip_header, udp_header and data. struct ethhdr ...
  1. #1
    Just Joined!
    Join Date
    May 2008
    Posts
    16

    Store MAC Address in char []

    Hi,

    I would like to know a way by which I can store the host address in char array.

    For example,
    //packet contains eth_header, ip_header, udp_header and data.

    struct ethhdr *ethernet_header;
    ethernet_header=(struct ethhdr*)(packet);

    Hence, the source MAC can be retrieved from 'ethernet_header->h_source' but i would like to store to in hex format in a char []. Suggest a possible solution.

    Thanks

  2. #2
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    If you know where its store and its length = size then its just a matter of copying it over to your character array ch[size]...byte by byte...Gerard4143
    Make mine Arch Linux

  3. #3
    Just Joined!
    Join Date
    May 2008
    Posts
    16
    Consider the following case,

    unsigned char *host_address;
    'host_address' does point to an address.

    on execution of the following snippet:

    for(int i=0;i<6;i++){
    printf("%.2x ",*host_address);
    host_address++;
    }

    the output displayed is: 00 11 25 b7 20 23

    Now I want this output to be stored in char *s, such that when i perform
    printf("%s", variable_name);
    Required Output should be:001125b72023

    Probable solution may implement sprintf() method.
    Please help!!

  4. #4
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    you have a space in your printf formatted string......"%.2x ".....it should be "%.2x"

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char *mystr = "gerard\n";
    
    int main(int argc, char**argv)
    {
    	int i;
    	for(i = 0;i < 5; i++)
    	{
    		printf("%.2x",*mystr);
    		++mystr;
    	}
    	exit(EXIT_SUCCESS);
    }
    Make mine Arch Linux

  5. #5
    Just Joined!
    Join Date
    May 2008
    Posts
    16
    ok Gerard but my question is still not answered.

    How do you store it HEX equivalents in char *s, such that they can be displayed on,

    printf("%s",variable_name);

    Please let me know in case I am not able put forward my query clearly.

  6. #6
    Linux Enthusiast gerard4143's Avatar
    Join Date
    Dec 2007
    Location
    Canada, Prince Edward Island
    Posts
    714
    quickly.....this will send the values to a file.....the rest you can figure out...Gerard4143


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char *mystr = "gerard\n";
    
    int main(int argc, char**argv)
    {
    	FILE *fd;
    	if (!(fd = fopen("gerard", "w")))
    	{
    		fputs("could not open gerard!\n", stderr);
    		exit(EXIT_FAILURE);
    	}
    
    	int i;
    	for(i = 0;i < 5; i++)
    	{
    		printf("%.2x",*mystr);
    		fprintf(fd, "%.2x",*mystr);
    		++mystr;
    	}
    
    	fclose(fd);
    	exit(EXIT_SUCCESS);
    }
    Make mine Arch Linux

  7. #7
    Just Joined!
    Join Date
    May 2008
    Posts
    16
    Thanks....
    What about the usage of sprintf? Can that be done?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...