Re: Is there a standard way to write an 8 bit byte?



Malcolm wrote:
"Kristian Nybo" <kristian@xxxxxxxxxxxxxxxxxx> 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. 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 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.


Just use fgetc and fputc to write integers between 0 and 255.

There is no cast iron guarantee in the C standard that this will produce the file you want, if say chars are 9 bits wide. However in practise there will always be an easy way to convert such files so that they can be read by 8-bit machines (maybe by running them though a converter program).. Computers exist in the real world and have to communicate with each other.

Ah, I see. Although there may exist a platform where that code doesn't result in a file composed of eight bit bytes, all the other files native to such a platform would share this incompatibility with 8-bit machines, so the platform would either be completely isolated, in which case there's no problem, or it would have to have a way of converting its files into files that *are* composed of eight bit bytes. Great.



Kristian .



Relevant Pages

  • Re: Patch of a new driver for kernel 2.4.x that need review
    ... On Wed, 22 Jun 2005, Bouchard, Sebastien wrote: ... > the new ATCA platform. ... Indent this variable properly with a tab. ... "char" which may or may not be signed depending on platform. ...
    (Linux-Kernel)
  • Re: filesize of an ofstream
    ... >in a platform specific support group for that. ... there is no guarantee that the value provided by that ... >function will equal the number of characters that you can read from ... access violations trying to use those functions (they're protected, ...
    (comp.lang.cpp)
  • Re: about declaration of huge matrix in C
    ... In this case, they don't matter. ... and C90 doesn't guarantee anything over ... no matter what the compiler or platform. ... "A strictly conforming program shall use only those features of the ...
    (comp.lang.c)
  • Re: using a freed pointer
    ... char *p1 = malloc; ... Consider a platform that has separate data and address registers. ... the passed-in pointer from the address space. ... offset 0xff00 is greater than the offset 0x0010. ...
    (comp.lang.c)
  • Re: Stupid programing question
    ... a char was 9 bits long on selected platforms. ... char is whatever suits the platform, ... > will automatically pack boolean's, or more to the point, automatically ... Enterprise Technology Solutions, TD Bank Financial Group ...
    (alt.os.linux)