Unoffical empeg BBS

Quick Links: Empeg FAQ | RioCar.Org | Hijack | BigDisk Builder | jEmplode | emphatic
Repairs: Repairs

Topic Options
#64272 - 29/01/2002 09:37 C Programming Question
tonyc
carpal tunnel

Registered: 27/06/1999
Posts: 7058
Loc: Pittsburgh, PA
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.
_________________________
- Tony C
my empeg stuff

Top
#64273 - 29/01/2002 09:50 Re: C Programming Question [Re: tonyc]
mlord
carpal tunnel

Registered: 29/08/2000
Posts: 14496
Loc: Canada

static int write_int (int fd, int value)
{
value = htonl(value);
return write(fd, &value, sizeof(value));
}

Top
#64274 - 29/01/2002 10:02 Re: C Programming Question [Re: mlord]
tonyc
carpal tunnel

Registered: 27/06/1999
Posts: 7058
Loc: Pittsburgh, PA
Splendid! Thanks, Mark.
_________________________
- Tony C
my empeg stuff

Top