From: Greg C. <chi...@mi...> - 2003-09-11 01:49:32
|
"Henning, Brian" wrote: > > I am trying to store 4 unsigned char's in an unsigned long with the following code. > Is there a better way of doing this other than the way i have presented? > > long result; > if( fread(bytes, sizeof(unsigned char), 4, ifp) != 4 ) { BTW, the language defines sizeof(unsigned char) to be 1. Using sizeof here doesn't make the code more portable. Maybe you want CHAR_BIT instead of 8 below, though. But if you want the data files to be portable, then byte order will get in the way. > fprintf(stderr, "Cannot read the bytes of this file\n"); > exit(1); > } > result = (result << 8) | bytes[3]; > result = (result << 8) | bytes[2]; > result = (result << 8) | bytes[1]; > result = (result << 8) | bytes[0]; > height = result; Wouldn't this fread(&result, sizeof(long), 1, ifp) do the same thing more simply? |