Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Just a couple points:

  1. Endian-ness is not generally based on the Operating System but on the processor. For example, Intel x86 processors are little-endian regardless of it running Windows or Linux.

  2. Your code will always return HL_LITTLE_ENDIAN. Why? Because if

     static constexpr uint8_t endianValues[4] = {0, 1, 2, 3};
    

then endianValues[0] == 0 will always be true! Suppose you had

    char x[4] = {'c','o','d', 'e'};

Don't you think it would be shocking if x[0] == 'e' instead of x[0] == 'c'?

The standard way is to use a union. Something like this:

    union endian_tester {
        uint32_t   n;
        uint8_t    p[4];
    };

    const endian_tester sample = {0x01020304}; // this initializes .n

    constexpr hl_endianness getEndianOrder() {
        return
            (0x04 == sample.p[0])               // If Little Endian Byte Order,
                ? HL_LITTLE_ENDIAN              
                : (0x01 == sample.p[0])         // Else if Big Endian Byte Order,
                    ? HL_BIG_ENDIAN             
                    : (0x02 == sample.p[0])     // Else if PDP Endian Byte Order,
                           ...(etc)...

Be aware that constexpr isn't fully supported in my version of Visual Studio 2013 Express.

  1. Not clear to me why you need to use fancy values for HL_LITTLE_ENDIAN, HL_BIG_ENDIAN, etc. You can use 1, 2, etc instead of 0x03020100, 0x00010203, etc.

  2. A related question answered on Stack Overflow (Detecting endianness programmatically in a C++ programDetecting endianness programmatically in a C++ program)

Just a couple points:

  1. Endian-ness is not generally based on the Operating System but on the processor. For example, Intel x86 processors are little-endian regardless of it running Windows or Linux.

  2. Your code will always return HL_LITTLE_ENDIAN. Why? Because if

     static constexpr uint8_t endianValues[4] = {0, 1, 2, 3};
    

then endianValues[0] == 0 will always be true! Suppose you had

    char x[4] = {'c','o','d', 'e'};

Don't you think it would be shocking if x[0] == 'e' instead of x[0] == 'c'?

The standard way is to use a union. Something like this:

    union endian_tester {
        uint32_t   n;
        uint8_t    p[4];
    };

    const endian_tester sample = {0x01020304}; // this initializes .n

    constexpr hl_endianness getEndianOrder() {
        return
            (0x04 == sample.p[0])               // If Little Endian Byte Order,
                ? HL_LITTLE_ENDIAN              
                : (0x01 == sample.p[0])         // Else if Big Endian Byte Order,
                    ? HL_BIG_ENDIAN             
                    : (0x02 == sample.p[0])     // Else if PDP Endian Byte Order,
                           ...(etc)...

Be aware that constexpr isn't fully supported in my version of Visual Studio 2013 Express.

  1. Not clear to me why you need to use fancy values for HL_LITTLE_ENDIAN, HL_BIG_ENDIAN, etc. You can use 1, 2, etc instead of 0x03020100, 0x00010203, etc.

  2. A related question answered on Stack Overflow (Detecting endianness programmatically in a C++ program)

Just a couple points:

  1. Endian-ness is not generally based on the Operating System but on the processor. For example, Intel x86 processors are little-endian regardless of it running Windows or Linux.

  2. Your code will always return HL_LITTLE_ENDIAN. Why? Because if

     static constexpr uint8_t endianValues[4] = {0, 1, 2, 3};
    

then endianValues[0] == 0 will always be true! Suppose you had

    char x[4] = {'c','o','d', 'e'};

Don't you think it would be shocking if x[0] == 'e' instead of x[0] == 'c'?

The standard way is to use a union. Something like this:

    union endian_tester {
        uint32_t   n;
        uint8_t    p[4];
    };

    const endian_tester sample = {0x01020304}; // this initializes .n

    constexpr hl_endianness getEndianOrder() {
        return
            (0x04 == sample.p[0])               // If Little Endian Byte Order,
                ? HL_LITTLE_ENDIAN              
                : (0x01 == sample.p[0])         // Else if Big Endian Byte Order,
                    ? HL_BIG_ENDIAN             
                    : (0x02 == sample.p[0])     // Else if PDP Endian Byte Order,
                           ...(etc)...

Be aware that constexpr isn't fully supported in my version of Visual Studio 2013 Express.

  1. Not clear to me why you need to use fancy values for HL_LITTLE_ENDIAN, HL_BIG_ENDIAN, etc. You can use 1, 2, etc instead of 0x03020100, 0x00010203, etc.

  2. A related question answered on Stack Overflow (Detecting endianness programmatically in a C++ program)

Used better markdown
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237
  1. Endian-ness is not generally based on the Operating System but on the processor. For example, Intel x86 processors are little-endian regardless of it running Windows or Linux.

    Endian-ness is not generally based on the Operating System but on the processor. For example, Intel x86 processors are little-endian regardless of it running Windows or Linux.

  2. Your code will always return HL_LITTLE_ENDIAN. Why? Because if

    static constexpr uint8_t endianValues[4] = {0, 1, 2, 3};

    then endianValues[0] == 0

    Your code will always be true! Suppose you had char x[4] = {'c','o','d', 'e'}; then don't you think it would be shocking if x[0] == 'e' instead ofreturn x[0] == 'c'HL_LITTLE_ENDIAN?

    The standard way is to use a union

    . Something like this: Why? Because if

    
    union endian_tester {
        uint32_t   n;
      static constexpr uint8_t    p[4];
    };
    
    const endian_tester sampleendianValues[4] = {0x01020304}; // this initializes .n
    
    constexpr hl_endianness getEndianOrder() {
        return
            (0x04 == sample.p[0])               // If Little Endian Byte Order0,
                ? HL_LITTLE_ENDIAN              
                : (0x01 == sample.p[0])         // Else if Big Endian Byte Order1,
                    ? HL_BIG_ENDIAN             
                    : (0x02 == sample.p[0])     // Else if PDP Endian Byte Order2,
                           ...(etc)...3};
    

    Be aware that constexpr isn't fully supported in my version of Visual Studio 2013 Express

  3. Not clear to me why you need to use fancy values for HL_LITTLE_ENDIAN, HL_BIG_ENDIAN, etc. You can use 1, 2, etc instead of 0x03020100, 0x00010203, etc.
  4. A related question answered in stackoverflow (Detecting endianness programmatically in a C++ program)

then endianValues[0] == 0 will always be true! Suppose you had

    char x[4] = {'c','o','d', 'e'};

Don't you think it would be shocking if x[0] == 'e' instead of x[0] == 'c'?

The standard way is to use a union. Something like this:

    union endian_tester {
        uint32_t   n;
        uint8_t    p[4];
    };

    const endian_tester sample = {0x01020304}; // this initializes .n

    constexpr hl_endianness getEndianOrder() {
        return
            (0x04 == sample.p[0])               // If Little Endian Byte Order,
                ? HL_LITTLE_ENDIAN              
                : (0x01 == sample.p[0])         // Else if Big Endian Byte Order,
                    ? HL_BIG_ENDIAN             
                    : (0x02 == sample.p[0])     // Else if PDP Endian Byte Order,
                           ...(etc)...

Be aware that constexpr isn't fully supported in my version of Visual Studio 2013 Express.

  1. Not clear to me why you need to use fancy values for HL_LITTLE_ENDIAN, HL_BIG_ENDIAN, etc. You can use 1, 2, etc instead of 0x03020100, 0x00010203, etc.

  2. A related question answered on Stack Overflow (Detecting endianness programmatically in a C++ program)

  1. Endian-ness is not generally based on the Operating System but on the processor. For example, Intel x86 processors are little-endian regardless of it running Windows or Linux.
  2. Your code will always return HL_LITTLE_ENDIAN. Why? Because if

    static constexpr uint8_t endianValues[4] = {0, 1, 2, 3};

    then endianValues[0] == 0

    will always be true! Suppose you had char x[4] = {'c','o','d', 'e'}; then don't you think it would be shocking if x[0] == 'e' instead of x[0] == 'c'?

    The standard way is to use a union

    . Something like this:
    
    union endian_tester {
        uint32_t   n;
        uint8_t    p[4];
    };
    
    const endian_tester sample = {0x01020304}; // this initializes .n
    
    constexpr hl_endianness getEndianOrder() {
        return
            (0x04 == sample.p[0])               // If Little Endian Byte Order,
                ? HL_LITTLE_ENDIAN              
                : (0x01 == sample.p[0])         // Else if Big Endian Byte Order,
                    ? HL_BIG_ENDIAN             
                    : (0x02 == sample.p[0])     // Else if PDP Endian Byte Order,
                           ...(etc)...
    

    Be aware that constexpr isn't fully supported in my version of Visual Studio 2013 Express

  3. Not clear to me why you need to use fancy values for HL_LITTLE_ENDIAN, HL_BIG_ENDIAN, etc. You can use 1, 2, etc instead of 0x03020100, 0x00010203, etc.
  4. A related question answered in stackoverflow (Detecting endianness programmatically in a C++ program)
  1. Endian-ness is not generally based on the Operating System but on the processor. For example, Intel x86 processors are little-endian regardless of it running Windows or Linux.

  2. Your code will always return HL_LITTLE_ENDIAN. Why? Because if

     static constexpr uint8_t endianValues[4] = {0, 1, 2, 3};
    

then endianValues[0] == 0 will always be true! Suppose you had

    char x[4] = {'c','o','d', 'e'};

Don't you think it would be shocking if x[0] == 'e' instead of x[0] == 'c'?

The standard way is to use a union. Something like this:

    union endian_tester {
        uint32_t   n;
        uint8_t    p[4];
    };

    const endian_tester sample = {0x01020304}; // this initializes .n

    constexpr hl_endianness getEndianOrder() {
        return
            (0x04 == sample.p[0])               // If Little Endian Byte Order,
                ? HL_LITTLE_ENDIAN              
                : (0x01 == sample.p[0])         // Else if Big Endian Byte Order,
                    ? HL_BIG_ENDIAN             
                    : (0x02 == sample.p[0])     // Else if PDP Endian Byte Order,
                           ...(etc)...

Be aware that constexpr isn't fully supported in my version of Visual Studio 2013 Express.

  1. Not clear to me why you need to use fancy values for HL_LITTLE_ENDIAN, HL_BIG_ENDIAN, etc. You can use 1, 2, etc instead of 0x03020100, 0x00010203, etc.

  2. A related question answered on Stack Overflow (Detecting endianness programmatically in a C++ program)

Source Link

Just a couple points:

  1. Endian-ness is not generally based on the Operating System but on the processor. For example, Intel x86 processors are little-endian regardless of it running Windows or Linux.
  2. Your code will always return HL_LITTLE_ENDIAN. Why? Because if

    static constexpr uint8_t endianValues[4] = {0, 1, 2, 3};

    then endianValues[0] == 0 will always be true! Suppose you had char x[4] = {'c','o','d', 'e'}; then don't you think it would be shocking if x[0] == 'e' instead of x[0] == 'c'?

    The standard way is to use a union. Something like this:

    
    union endian_tester {
        uint32_t   n;
        uint8_t    p[4];
    };
    
    const endian_tester sample = {0x01020304}; // this initializes .n
    
    constexpr hl_endianness getEndianOrder() {
        return
            (0x04 == sample.p[0])               // If Little Endian Byte Order,
                ? HL_LITTLE_ENDIAN              
                : (0x01 == sample.p[0])         // Else if Big Endian Byte Order,
                    ? HL_BIG_ENDIAN             
                    : (0x02 == sample.p[0])     // Else if PDP Endian Byte Order,
                           ...(etc)...
    

    Be aware that constexpr isn't fully supported in my version of Visual Studio 2013 Express

  3. Not clear to me why you need to use fancy values for HL_LITTLE_ENDIAN, HL_BIG_ENDIAN, etc. You can use 1, 2, etc instead of 0x03020100, 0x00010203, etc.
  4. A related question answered in stackoverflow (Detecting endianness programmatically in a C++ program)