t_fourcc typedef and endianness


    Feb 23 2014 | 8:40 pm
    Hello,
    This is probably more of a general C question rather than max/msp.
    In Max SDK 6.1.4 there is a typedef t_fourcc which is a 32 bit uint. It is there for conveniently storing 4 characters as I understand.
    I know that: ASCII hex 'M' = 6d 'T' = 54 'h' = 68 'd' = 64
    Now if I do this t_fourcc midi = 'MThd';
    is my value stored as 6d 54 68 64 starting from the smaller address name irrelevant of machine endianness? In contrast, for example, uint = 4 will be stored differently on a little endian machine and big endian machine (right?)

    • Jul 21 2014 | 2:03 pm
      To correctly handle endianess with four char codes you can byteswap as follows. Note that in this case typestr is 5 chars long to accomodate the NULL termination.
      void fourcc_from_int(char *typestr, t_fourcc type)
      {
      	t_int32			type_swapped;
      #if C74_LITTLE_ENDIAN
      	type_swapped = BYTEORDER_SWAPW32(type);
      #else
      	type_swapped = type;
      #endif
      	strncpy_zero(typestr, (char*)(&type_swapped), 5);
      }