Re: Is there a standard way to write an 8 bit byte?
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Mon, 02 May 2005 22:58:21 +0100
Kristian Nybo wrote:
I'm writing a simple image file exporter as part of a school project, and I would like to write completely platform-independent code if at all possible.
A laudable aim, and people here will be happy to help with anything that can be done in standard C.
> The problem I've run into is that apparently there's no
guarantee that a particular implementation will have a data type that is exactly 8 bits wide; char is only guaranteed to be at least 8 bits.
Correct.
Correct me if I'm wrong, but I assume that if CHAR_BIT happens to be, say, 9, writing a char into a file in binary mode would result in 9 bits being written.
In the simple case, yes. However, if the file is on a file system with 8 bit bytes it might have to do something very strange. Think about what could happen if the file was on a network share from a Windows box.
At first it seemed that stdint.h and its uint8_t were the answer, but then I noticed that uint8_t is only guaranteed to exist if the platform has a data type that is exactly 8 bits wide, which put me back in square one.
Correct.
Is there a way around this, or will I just have to use uint8_t and accept that my code might not work on every conceivable platform that has an ANSI C compiler?
You should try to isolate as much as possible the code that depending on having a type exactly 8 bits wide and use uint_8 for that. Possibly even putting it in a separate file.
Where you need at least 8 bits, but do not actually care how many bits, you could use uint_least8_t which will be 8 bits on systems that support it, but larger where not supported.
You should also understand that stdint.h was added by the C99 standard which is not supported by most compilers. However, it is easy enough to implement what you need of it on any given system, so I would still use them and implement them for any target that did not have stdint.h
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
.
- References:
- Is there a standard way to write an 8 bit byte?
- From: Kristian Nybo
- Is there a standard way to write an 8 bit byte?
- Prev by Date: Re: Is there a standard way to write an 8 bit byte?
- Next by Date: Re: unsigned wierdness
- Previous by thread: Re: Is there a standard way to write an 8 bit byte?
- Next by thread: Re: Is there a standard way to write an 8 bit byte?
- Index(es):
Relevant Pages
|