Conversion: uid_t to string, off_t to string
Solution 1
Both are defined as arithmetic types (http://www.opengroup.org/onlinepubs/009695399/basedefs/sys/types.h.html), and in practice are positive and integral. So you can just cast to unsigned long long, and use sprintf with "%llu" to convert to string.
Solution 2
size_t
and off_t
are just unsigned integral types. (Edit: off_t
is a long? See, the lesson is, check your headers!)
So use sprintf
(or whatever) to convert them using the "%i" format specifier.
On edit: crap, you changed size_t
to uid_t
while I was answering. uid_t
is defined in types.h
; look there. (It's also an unsigned integral type, but an unsigned short
.)
Solution 3
Linux' snprintf() supports the 'z' format specifier for values of type size_t
. Not sure how portable this is, you'll need to inspect the "CONFORMS TO" section closely.
For off_t
, you might need to cast to the largest unsigned integer type, i.e. unsigned long
and use a "lu" specifier.
Comments
-
israkir almost 2 years
I am currently writing a systems programming homework and in one part i need to get some information of a file in a directory.
for the stat of file, we have
ctime()
function which convertstime_t
type tostring
and returns a pointer to it.but how about the
uid_t
andoff_t
types? I searched through to internet and couldnt find any function.. Or if there does not exist any function, can you tell me how to implement such a function please? -
Matthew Flaschen about 15 yearsI don't believe they're guaranteed to be those types on all platforms.
-
Matthew Flaschen about 15 yearsI think the actual lesson is that these are different sizes on different systems.
-
Matthew Flaschen about 15 yearsWhy can't you just widen the values to unsigned long long?
-
tpdi about 15 yearsYes, which is why I wrote "check your headers". Emphasis on "your". :)