473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Converting int of unknown length to a string

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
May 10 '06 #1
11 3332
TomServo wrote:
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.

--
Ian Collins.
May 10 '06 #2
Ian Collins wrote:
TomServo wrote:
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.


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

May 10 '06 #3
I think you can define a struct;
struct myInteger {
BYTE type; // express data type. int or long;
union {
int i;
long i;
} Data;
}

May 10 '06 #4
> 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?
May 10 '06 #5
TomServo wrote:
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

May 10 '06 #6
TomServo wrote:
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.


I was referring to the lltostr function.

Robert Gamble

May 10 '06 #7
"TomServo" <to*******@yahoo.com> writes:
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.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
May 10 '06 #8
On Tue, 09 May 2006 19:05:27 -0600, TomServo wrote:
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.
May 10 '06 #9
William Ahern <wi*****@25thandClement.com> writes:
On Tue, 09 May 2006 19:05:27 -0600, TomServo wrote:
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.


If he can assume C99, then he can assume support for long long,
so there's no problem.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
May 10 '06 #10
On Tue, 9 May 2006 20:09:37 -0600,
TomServo <to*******@yahoo.com> wrote
in Msg. <nv********************@giganews.com>
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
May 10 '06 #11
On Tue, 09 May 2006 20:50:46 -0700, Ben Pfaff wrote:
William Ahern <wi*****@25thandClement.com> writes:
On Tue, 09 May 2006 19:05:27 -0600, TomServo wrote:
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.


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.

May 10 '06 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
2
by: Pablo | last post by:
I have a dilemma. Currently, I may be passing standard text (strings of char) or binary of 1 to 'x' bytes long to a program for comparison with data previously written to a file. The problem...
7
by: jj | last post by:
It seems simple but I had a hard time converting this small function from vb.net to C#. I even tried the software that automatically converts from vb to c# , but to no avail. Please can someone...
0
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual...
2
by: Jason James | last post by:
Guys, can anyone confirm the process on converting the OID into an array of bytes for sending to the SNMP device. The code I have seems to only work for values in the OID that are less than...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
15
by: allthecoolkidshaveone | last post by:
I want to convert a string representation of a number ("1234") to an int, with overflow and underflow checking. Essentially, I'm looking for a strtol() that converts int instead of long. The...
3
by: Dhananjay | last post by:
Hi All, I am facing problem when i am converting C#.net code(Delegate concept) into vb.net. I am unable to do that . Can someone help me to solve the problem. I am providing my C#.net code. ...
6
by: ogtheterror | last post by:
Hi I have a very limited understanding of Python and have given this the best shot i have but still have not been able to get it working. Is there anyone that knows how to get this into a .net...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.