have a buffer of type integer (4 bytes) with 1000 elements. what is quickest method to typecast each element to short (2 bytes) before writing it a binary opened file?.
Printable View
have a buffer of type integer (4 bytes) with 1000 elements. what is quickest method to typecast each element to short (2 bytes) before writing it a binary opened file?.
A non-portable (doesn't work for big-endian machines) could be:An endian independent version requires a temporary buffer.Code:int32 arr[1000];
FILE* pF;
for( int i = 0; i < 1000; ++i ) fwrite( &arr[i], 2, 1, pF );
Edit: I assume that you're sure that the elements only contains "16-bit values"?
Why not just cast explicitly with "(short)" ?