I have an unsigned integer that I need to save to a file. It represents a timecode in milliseconds. The file format I'm saving in (ID3v2 actually) specifies that the timestamp is a 32-bit (4 byte) integer. So in my C program, I need to find a way to write that integer to the file. The LSB's need to be on the right, MSB's to the left... So the number 511 would be represented as 0x00 0x00 0x01 0xFF.
My first thought was to use "write(fd, &a, sizeof(unsigned long));" but that outputs the bytes reversed (511 shows up as 0xFF 0x01 0x00 0x00). I'm terrible at bitwise manipulation and such, so I couldn't figure out how to make this work right.
The other thing is I would like it to behave the same on different platforms, i.e. I'd like to not worry about endian-ness and stuff. I'll trade some performance for something that's more portable. (Anyone telling me to use Java will be smacked.)
So can anyone think of a way to do this? It seems simple. 32 bit integer value, 32 bits in the file, yet I can't get it to work.
This is all part of my efforts to use the SYLT (SYnchronized Lyrics/Text) ID3v2 frame to hold song lyrics and timecodes. I thought that the ID3lib might have some nice convenient utility functions to do this kind of stuff, but I couldn't find them... So I'm rolling my own.
Any help is appreciated.