CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2005
    Posts
    78

    char array and endian concern

    Hi all,

    I got 2 programs running on 2 different machines.
    program A set some configs to buffer and sends the buffer to program B. B read the buffer and display the config.

    A and B both cast the buffer to a structure and then access structure's fields.

    char buffer[128];

    struct xx
    {
    int32 i;
    int32 j;
    char str[64];
    }

    For example:
    xx *ptr= (xx*)bufffer;
    xx->i = 1000;
    xx->j= 6742;



    So far I don't have problem because both machines running on PPC (big endian).
    Now if A has also to be run on an Intel (little endian), what should I do to take care of this?


    thanks in advance.

  2. #2
    Join Date
    Jul 2009
    Posts
    6

    Re: char array and endian concern

    What you need to do is convert the integers in your struct to network byte order before sending, then back to host byte order after you receive it. Since the int fields are 32 bit, use htonl() to switch to network byte order and ntohl to switch to host byte order.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured