Converting int of unknown length to a string

T

TomServo

I am writing code that needs to run on a variety of Unix systems. I am
calling the statvfs and statfs system calls and I need to to convert some of
the integers returned to character strings. Normally I would do this using
sprintf as so:
sprintf(&buffer, "%lu", integer);

The problem is that I will not know if the integer values I am converting
are long or long long. I also do not know if the compilers being used will
support long long ints. So is there any way to convert a unknown integer to
a string without specifying what kind of integer?

Will itoa() work on a long long? If it doesn't, are there any calls like it
that will work on any int passed to it?

To reiterate what I need to do, here is some pseudo code:

struct statfs fsstruct;
statfs("/", &fsstruct);
// Take fsstruct.f_blocks value which is an int of unknown length and change
it to a string
// fsstruct.f_blocks could be a short int, long int, long long int, etc
// I also have no idea if long longs are supported

Am I making this harder than it is?

Thanks
 
I

Ian Collins

TomServo said:
I am writing code that needs to run on a variety of Unix systems. I am
calling the statvfs and statfs system calls and I need to to convert some of
the integers returned to character strings. Normally I would do this using
sprintf as so:
sprintf(&buffer, "%lu", integer);

The problem is that I will not know if the integer values I am converting
are long or long long. I also do not know if the compilers being used will
support long long ints. So is there any way to convert a unknown integer to
a string without specifying what kind of integer?

Will itoa() work on a long long? If it doesn't, are there any calls like it
that will work on any int passed to it?
Try (u)lltostr, assuming you know whether your values are signed or not.
 
R

Robert Gamble

Ian said:
Try (u)lltostr, assuming you know whether your values are signed or not.

1. lltostr is not a Standard C function which would make it off-topic
here.
2. I have never seen this function on any system outside of Solaris so
I wouldn't call it very portable, even across Unix systems.
3. If the compiler does not support type long long int then a function
that returns a long long value probably won't be callable with such a
compiler.

Robert Gamble
 
J

jefong

I think you can define a struct;
struct myInteger {
BYTE type; // express data type. int or long;
union {
int i;
long i;
} Data;
}
 
T

TomServo

1. lltostr is not a Standard C function which would make it off-topic
here.
2. I have never seen this function on any system outside of Solaris so
I wouldn't call it very portable, even across Unix systems.
3. If the compiler does not support type long long int then a function
that returns a long long value probably won't be callable with such a
compiler.

Robert Gamble


1. Yeah whatever I use has to be standard.

2. The code that uses statfs only gets called if statfs is defined on the
system, so that is covered. Same with statvfs.

3. I don't know what to do about item number 3. :-(


On a side note, I was looking on the K&R book and they have some code that
implements a itoa function. I guess I could just write my own that could
work on varible length ints. I can do a test to see if it is signed and I
can get the length from sizeof(). That would be a pain in the arse though.
You would think there would be some easier way to do things?
 
R

Robert Gamble

TomServo said:
I am writing code that needs to run on a variety of Unix systems. I am
calling the statvfs and statfs system calls and I need to to convert some of
the integers returned to character strings. Normally I would do this using
sprintf as so:
sprintf(&buffer, "%lu", integer);

The problem is that I will not know if the integer values I am converting
are long or long long. I also do not know if the compilers being used will
support long long ints. So is there any way to convert a unknown integer to
a string without specifying what kind of integer?

Not portably.
Will itoa() work on a long long? If it doesn't, are there any calls like it
that will work on any int passed to it?

itoa isn't a Standard C function but based on the name I assume it
converts an "int" to "ascii". If this is the case then passing a wider
type than int to the function isn't a wise idea.
To reiterate what I need to do, here is some pseudo code:

struct statfs fsstruct;
statfs("/", &fsstruct);
// Take fsstruct.f_blocks value which is an int of unknown length and change
it to a string
// fsstruct.f_blocks could be a short int, long int, long long int, etc
// I also have no idea if long longs are supported

Am I making this harder than it is?

If you don't want to write seperate code for each scenerio and you know
that the value of f_blocks can be represented by an unsigned long int,
then your best bet might be to cast the value to unsigned long and use
sprintf to convert it.

Robert Gamble
 
R

Robert Gamble

TomServo said:
1. Yeah whatever I use has to be standard.

2. The code that uses statfs only gets called if statfs is defined on the
system, so that is covered. Same with statvfs.

I was referring to the lltostr function.

Robert Gamble
 
B

Ben Pfaff

TomServo said:
The problem is that I will not know if the integer values I am converting
are long or long long. I also do not know if the compilers being used will
support long long ints. So is there any way to convert a unknown integer to
a string without specifying what kind of integer?

Here is an approach that might work:

#include <limits.h>

#ifdef LLONG_MAX
typedef long long big_type;
#define BIG_FMT "ll"
#else
typedef long big_type;
#define BIG_FMT "l"
#endif

...
printf ("%"BIG_FMT"d", (big_type) big_value);
...

Unfortunately, I think it'll probably fail in practice due to
differences between compiler and library support for long long.
 
W

William Ahern

I am writing code that needs to run on a variety of Unix systems. I am
calling the statvfs and statfs system calls and I need to to convert some of
the integers returned to character strings. Normally I would do this using
sprintf as so:
sprintf(&buffer, "%lu", integer);

The problem is that I will not know if the integer values I am converting
are long or long long. I also do not know if the compilers being used will
support long long ints. So is there any way to convert a unknown integer to
a string without specifying what kind of integer?

If you can at least assume C99, cast to uintmax_t or intmax_t and use the
%ju and %jd format specifiers.

Alternatively, you could use %zu and %zd which correspond to size_t and,
on Unix, ssize_t. Even if the %j qualifier isn't supported there's a
better chance %z will be.
 
B

Ben Pfaff

William Ahern said:
If you can at least assume C99, cast to uintmax_t or intmax_t and use the
%ju and %jd format specifiers.

If he can assume C99, then he can assume support for long long,
so there's no problem.
 
R

Robert Latest

On Tue, 9 May 2006 20:09:37 -0600,
in Msg. said:
1. Yeah whatever I use has to be standard.

2. The code that uses statfs only gets called if statfs is defined on the
system, so that is covered. Same with statvfs.

Since your program needs the information provided by statfs on some
systems, it'll have to come by that information on other systems in
other ways, which means that you must make such distinctions at build
time anyway. What's stopping you from checking the widths of the
available int types and those of the struct statfs members with sizeof?

Apart from that, the intmax_t type in C99 was implemented for just this
reason I guess.

robert
 
W

William Ahern

If he can assume C99, then he can assume support for long long,
so there's no problem.

Yep. This occurred to me exactly one moment after I posted ;)

OTOH, in my experience %z seems to have been one of the earliest supported
C99 "features". So if the OP can live with size_t (or Unix's ssize_t) as
a common type, then that's a possible alternative.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top