473,774 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_bloc ks value which is an int of unknown length and change
it to a string
// fsstruct.f_bloc ks 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 3372
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_bloc ks value which is an int of unknown length and change
it to a string
// fsstruct.f_bloc ks 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*******@yaho o.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[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof 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*****@25than dClement.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[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
May 10 '06 #10

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

Similar topics

5
13928
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 something like: char chars = "Hello!"; byte bytes = (byte) chars;
2
1965
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 I'm having is I'm writing some routines which may compare the data written on the disk to the some data being passed by the programmer. And, the streams I'm passing may be shorter than the allowable length limit (defined at runtime) but never...
7
2056
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 take some precious minutes to help me out. Thanks John Public Function SimpleCrypt( ByVal Text As String) As String Dim strTempChar As String, i As Integer
0
2033
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 Studio 2005). The problem occurred when I trying to opening and reading file: ============= C++ ============= void LoadSourceFile(char * fileName, unsigned char * data, int length)
2
6995
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^14 but some enterprise IDs are now larger than that. Do I just contine the conversion on to additional bytes needed to house the OID in full? So and enterprise ID of 25000 would be sent as three bytes of the following:
2
4009
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 functions strtol, strtod, and strtoul, defined in <cstdlib>, convert a null- terminated character string to a long int, double, or unsigned long. You can use them to convert numeric strings of any base to a numeric
15
9670
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 problem with strtol() is that a number that fits into a long might be too big for an int. sscanf() doesn't seem to do the over/underflow checking. atoi(), of course, doesn't do any checking. I've long thought it odd that there aren't strtoi() and...
3
1817
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. ==================================my code is :- ====================================== static public List<MembershipUserWrapperGetMembers(bool
6
8399
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 assembly? _________________________________________________________ def encrypt_password(challenge, password): if challenge == 'md5': inner = md5(challenge + password).hexdigest() elif challenge == 'sha1': inner =...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10106
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10039
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7463
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6717
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.