Signed 'endian' ; Byte order conversion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • velpur
    New Member
    • Sep 2007
    • 1

    #1

    Signed 'endian' ; Byte order conversion

    Dear friends,

    ( Suppose we change byte orders. For example, our program
    reads a file as a character string and converts the byte order. )
    // a simple code

    #define INTEL_CPU
    #ifdef INTEL_CPU
    /* *************** ************ FlipLong() *************** ***************
    * Converts a long in "Big Endian" format (ie, Motorola 68000) to Intel
    * reverse-byte format, or vice versa if originally in Big Endian.
    *************** *************** *************** *************** *********
    */
    void FlipLong(unsign ed char * ptr) {
    register unsigned char val;

    /* Swap 1st and 4th bytes */
    val = *(ptr);
    *(ptr) = *(ptr+3);
    *(ptr+3) = val;

    /* Swap 2nd and 3rd bytes */
    ptr += 1;
    val = *(ptr);
    *(ptr) = *(ptr+1);
    *(ptr+1) = val;
    }
    #endif
    ----------------------------------------****** OR *****-------------------------------------------------

    A more efficient function can be implemented using bitwise operations as
    shown below:
    unsigned long ByteSwap2 (unsigned long nLongNumber)
    {
    return (((nLongNumber& 0x000000FF)<<24 )+((nLongNumber &0x0000FF00)<<8 )+
    ((nLongNumber&0 x00FF0000)>>8)+ ((nLongNumber&0 xFF000000)>>24) );
    }

    (Both the above samples are declares val/nLongNumber as unsigned char. )

    ---

    Tell me how to deal with sign, or give me sample code
    to convert byte orders with signed char.


    please any one help me out.


    regards,
    velpur
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    There's no special handling of the sign bit.

    For little endian, the sign bit is in the highest memory address byte whereas for big endian it is in the lowest memory address byte.

    In both cases, the sign bit is bit 7.

    Just keep the byte together.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      There are macros/functions for that might be useful: htonl, htons, ntohl and ntohs.
      The hton* functions convert a long or short from host to network order; network
      order is big endian order. The ntoh* convert from network order to host order.

      kind regards,

      Jos

      Comment

      • apollo135
        New Member
        • Sep 2007
        • 9

        #4
        Originally posted by JosAH
        There are macros/functions for that might be useful: htonl, htons, ntohl and ntohs.
        The hton* functions convert a long or short from host to network order; network
        order is big endian order. The ntoh* convert from network order to host order.

        kind regards,

        Jos

        -------------

        Hi Jos,

        I tried also to use macros: htonl and ntohl as the variables exchanged between client and server are defined in double. But it does not work ... and here is a peace of code I wrote:

        -------- client.cpp---
        ...
        typedef struct SIM_DATA_IN{
        // char svar_instg;
        double svar_integ[2];
        double svar_indl[5];
        };
        SIM_DATA_IN stus;
        ...
        stus.svar_integ[0]=htonl(var);
        stus.svar_integ[2]=htonl(mr);
        ...
        stus.svar_indl[1]=htonl(GH);
        ....
        stus.svar_indl[5]=htonl(DR);
        ....

        if(sendto(sd,&s tus,sizeof(&stu s),0,(struct sockaddr *) &pin,sizeof(pin ))) {
        perror("write data");
        exit(1);
        }
        .....

        --------server.cpp-------
        ...
        #define MAXBF 1024
        ....

        typedef struct SIM_DATA_IN{
        // char svar_instg;
        double svar_integ[2];
        double svar_indl[5];
        };
        SIM_DATA_IN stus;


        if(recvfrom(sd, &stus,MAXBF,0,( struct sockaddr *) &pin,&fromSize) ) {
        perror("read data");
        exit(1);
        }
        .....
        ...
        *spn=ntohl(stus .svar_integ[0]);
        *spm=ntohl(stus .svar_integ[1]);
        ...
        *MB=ntohl(stus. svar_indl[0]);
        ...
        *SB=ntohl(stus. svar_indl[5]);
        ....

        =============== =
        Is this correct? if yes, it does not work ... I don't get the same data at the server side ... What is the matter then?

        Regards and thanks for your reply,
        Azwaw

        Comment

        Working...