473,788 Members | 2,800 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
11 3373
On Tue, 9 May 2006 20:09:37 -0600,
TomServo <to*******@yaho o.com> wrote
in Msg. <nv************ ********@gigane ws.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*****@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.


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
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
6996
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
4010
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
8402
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
9656
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10172
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
10110
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,...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7517
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
5398
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
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4069
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
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.