[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: question for Russ - signed vs. unsigned char and NC_BYTE



Ed,

> Here's a question I have about the netcdf interface.
> 
> Suppose I approach an unknown file, which contains an attribute. I can
> use nc_inq_att to find out the type of the att. Suppose it is NC_BYTE.
> 
> How do I read that attribute? With nc_get_att_schar or with
> nc_get_att_uchar?
> 
> That is, is it signed or unsigned?
> 
> The netcdf manual says that when writing data out, NC_BYTE will be
> treated as signed. But then why do we have nc_get_att_uchar?

We have both functions so that users can read data into either signed
or unsigned arrays of char without requiring an ugly cast.  If we only
had nc_get_att_schar() and a user wanted to read NC_BYTE data into an
array of unsigned char, they would have to use a cast or get a
compiler complaint.

In either case the same 8 bits are read into the same location in
memory, but we have to provide both schar and uchar versions to allow
the user to treat byte data as either signed or unsigned.  No
conversion takes place reading/writing a signed or unsigned char in
memory to or from a byte on disk, so users can still treat NC_BYTE
data as unsigned char if they want to.  To allow them to do this
without a cast, we provide the convenience function.

For the same reason, we provide both nc_get_var_schar() and
nc_get_var_uchar(), and similarly for the corresponding put_var
functions.

--Russ