This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
> To: address@hidden > Subject: NetCDF help > Date: Mon, 04 Nov 96 10:32:22 -0500 > From: address@hidden Hi Chris, > I have what seems to be an easy problem but I can't come up with an > effective solution. I'm working with a C interface with netCDF and > have an array of unsigned chars. When I send the data to netCDF, it > interprets them as signed chars and therefore misinterprets the data > for larger values. > > Is there an easy way to make netCDF recognize unsigned char using a C > interface? Thanks. I'll need more information to help. What version of netCDF are you using? The latest released version (2.4.3) doesn't support any type conversions in the interface, so whatever 8-bit byte you store in an NC_BYTE type netCDF variable, you will get the exact same 8 bits back when you read data from that netCDF variable into a memory location. If that memory location is for a variable that's declared "unsigned char", it should be interpreted as unsigned. On the other hand, if you're using the netCDF-3.1a prerelease, there is a new interface that lets you read 8-bit data as other numeric types. For example, the function int nc_get_vara_int (int ncid, int varid, const size_t *startp, const size_t *countp, int *ip); will let you retrieve a variable as an int, whether it's stored as a byte, a short, an int, a long, a float, or a double. In that case, 8-bit values are interpreted as signed for the purpose of any numeric conversions that the library does. But you can still retrieve the data as unsigned bytes by using a different netCDF-3.1a function: int nc_get_vara_uchar (int ncid, int varid, const size_t *startp, const size_t *countp, unsigned char *ip); So you should be able to do what you want with either interface. Let me know if you still have questions about this. --Russ _____________________________________________________________________ Russ Rew UCAR Unidata Program address@hidden http://www.unidata.ucar.edu