koneu wrote: > If anything, I would store the numbers as unsigned 64 bit LSB and change the > read/write functions for MSB architectures.
Heyho, max(uint32_t) = 4.294.967.296 is already way more than the proposed max of 999.999.999. I would even suggest uint16_t is enough. To store images larger than 65536^2 (=16GiB with 4 bytes per pixel) is far enough in the future and also the argument with the need for greater color depth applies. We could just use htons and ntohs then: spec ---- Bytes Description 9 ASCII string: "imagefile" 2 Width of the image stored in network byte order (big-endian) 2 Height of the image stored in network byte order (big-endian) Then, (width*height) pixels arranged in height scanlines, where each pixel is four bytes. Each byte represents red, green, blue, and alpha respectively. This function reads an image: char * readimage(int fd, uint16_t *w, uint16_t *h) { char hdr[13]; char *data; int len; if (read(fd, hdr, 13) != 13 || strcmp(hdr, "imagefile")) return NULL; *w = ntohs(hdr[9]); *h = ntohs(hdr[11]); len = (*w) * (*h) * 4; if (!(data = malloc(len)) || read(fd, data, len) != len) { free(data); return NULL; } return data; } This function writes an image: int writeimage(int fd, uint16_t w, uint16_t h, char *data) { uint16_t nw = htons(w); uint16_t nh = htons(h); return (write(fd, "imagefile", 9) == 9 && write(fd, &nw, 2) == 2 && write(fd, &nh, 2) == 2 && write(fd, data, w*h*4) == w*h*4); } We also avoid *printf and stdio.h. --Markus