convert struct to host byte order

This is a discussion on convert struct to host byte order within the C Programming forums, part of the General Programming Boards category; Hi all, I want to convert a received struct from network byte order to host byte order (may be with ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    63

    convert struct to host byte order

    Hi all,

    I want to convert a received struct from network byte order to host byte order (may be with ntohs or ntohl functions), but i have a trouble to do so, my code is :

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <stdint.h>
    
    typedef struct packet {
    	uint8_t 	pack_type;
    	uint16_t	pack_crc;
    	uint32_t	pack_src;
    	uint8_t		pack_dest[10];
    	uint8_t		pack_data[5];
    } packet_t;
    
    packet_t * received_buffer(char *buffer) { // buffer is received from the network
    	
    	packet_t	*result;
    	
    	result = memcpy(result, buffer, sizeof(result));
    	
    	/* I want some code here to transforme all my struct from network byte order to host
    		byte order.
    	*/
    	
    	return result;
    }
    Thanks for your time and your help .

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You're going to have to use the appropriate ntoh_ for each element of your struct.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,515
    What did you try using ntohl and ntohs? What didn't work about it?

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    thanks tabstop,

    Quote Originally Posted by tabstop View Post
    You're going to have to use the appropriate ntoh_ for each element of your struct.
    for uint8_t how can i deal with that? by casting? and for an array of uint8_t also? ther's no direct method to transform the entire struct to the host byte order?

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,515
    It's irrelevant for a uint8_t. There is only one byte in there, so you can't switch the order of individual bytes.

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Thanks anduril462,

    Quote Originally Posted by anduril462 View Post
    What did you try using ntohl and ntohs? What didn't work about it?
    I want to transform the received struct to the HOST BYTE ORDER, because in the network the received buffer can have a different ENDIANESS.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by jean.yves View Post
    thanks tabstop,



    for uint8_t how can i deal with that? by casting? and for an array of uint8_t also? ther's no direct method to transform the entire struct to the host byte order?
    A uint8_t is one byte long. Do you think byte order is going to affect those variables?

    And there will be a direct method to transform the entire struct to host byte order, once you finish writing it.

  8. #8
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Quote Originally Posted by anduril462 View Post
    It's irrelevant for a uint8_t. There is only one byte in there, so you can't switch the order of individual bytes.
    Ok thanks and for the arrays (for example a WORD of bytes)?

  9. #9
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Quote Originally Posted by tabstop View Post
    A uint8_t is one byte long. Do you think byte order is going to affect those variables?

    And there will be a direct method to transform the entire struct to host byte order, once you finish writing it.
    Ok that's cool.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Endianness is only on a byte basis. If you have a bunch of single bytes, then they will be sent correctly because no switching occurs inside a byte.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,515
    Don't do anything. Byte order only matters for multi-byte types. An array of bytes, while taking up multiple bytes in memory, is really just a contiguous block of single-byte things. Each element is it's own uint8_t, and doesn't need to be reordered.

  12. #12
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Ok so how can i transform this fields :
    uint16_t pack_crc;
    uint32_t pack_src;
    to Host Byte Oreder?

  13. #13
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,515
    Assign them the value of ntohs(network_short) and ntohl(network_long)

  14. #14
    Registered User
    Join Date
    Aug 2010
    Posts
    63
    Thanks i will try and let you know .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error in Exceptions.h file during build of QuickFix library
    By arupsarkar in forum C++ Programming
    Replies: 3
    Last Post: 07-16-2010, 10:30 AM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  4. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-20-2009, 11:37 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21