473,395 Members | 1,457 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,395 software developers and data experts.

Converting long long to char

Hi,

I've need a function to print a long long to the console and needs to
run under Win32 and some unknown variant of *Nix (a client's
unspecified server)

in Win32 I've got

_int64 iVal
sprintf(cTemp, "Wrong length %I64d\n", iVal);

The control code (and the _int 64 type) aren't standard C
I'd rather use a runtime function that write m own...

Ta.
Nov 14 '05 #1
8 9930
silangdon wrote:
Hi,

I've need a function to print a long long to the console and needs to
run under Win32 and some unknown variant of *Nix (a client's
unspecified server)

in Win32 I've got

_int64 iVal
sprintf(cTemp, "Wrong length %I64d\n", iVal);

The control code (and the _int 64 type) aren't standard C
I'd rather use a runtime function that write m own...

Ta.


In standard C you can write:

long long iVal;
sprintf(cTemp,"Wrong length %lld\n",iVal);

long long is a standard C99 type, and its format is %lld
Nov 14 '05 #2
silangdon <me@you.twang> writes:
I've need a function to print a long long to the console and needs to
run under Win32 and some unknown variant of *Nix (a client's
unspecified server)


The standard length modifier for long long is `ll', as in `%lld'.
--
"To get the best out of this book, I strongly recommend that you read it."
--Richard Heathfield
Nov 14 '05 #3
silangdon wrote:
Hi,

I've need a function to print a long long to the console and needs to
run under Win32 and some unknown variant of *Nix (a client's
unspecified server)

in Win32 I've got

_int64 iVal
sprintf(cTemp, "Wrong length %I64d\n", iVal);

The control code (and the _int 64 type) aren't standard C
I'd rather use a runtime function that write m own...

Ta.


You can do the following if you need to for all platforms you support.
Details for any specific platform are off topic here, but here
is an example:

#if defined(_WIN32) /* Or some other MSVC specific define */
#define my_longlong _int64
#define PRINTF_LL "%I64d"
#else
#define my_longlong long long int
#define PRINTF_LL "%lld"
#endif

my_longlong iVal;
sprintf(cTemp, "Wrong length " PRINTF_LL "\n", iVal);

-David
Nov 14 '05 #4
jacob navia <ja***@jacob.remcomp.fr> writes:
silangdon wrote:
Hi, I've need a function to print a long long to the console and
needs to
run under Win32 and some unknown variant of *Nix (a client's
unspecified server)
in Win32 I've got _int64 iVal
sprintf(cTemp, "Wrong length %I64d\n", iVal);
The control code (and the _int 64 type) aren't standard C I'd rather
use a runtime function that write m own...
Ta.


In standard C you can write:

long long iVal;
sprintf(cTemp,"Wrong length %lld\n",iVal);

long long is a standard C99 type, and its format is %lld


Yes, but it's entirely possible that the OP needs to deal with one or
more implementations that don't support long long and/or the "%lld"
format. (I've seen systems that have one but not the other.)

C99 is the official standard, but pretending it's universal in the
real world doesn't make it so, and doesn't help people who need to
deal with pre-C99 implementations.

Certainly you should use long long and "%lld" on systems that support
them (and even many pre-C99 implementations do so), but there's still
often a need to use non-standard extensions to C90 to achieve the same
result.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
Sorry Keith but how would you implement long long in C89???

I mean, after having implemented it in lcc-win32 this is not
really easy :-)

If he hasn't long long then he is stuck. But he was speaking of
*nix system, and in most of them gcc is available, so I think your
fears aren't sustained by any real data. It would be surprising to find
a unix system without gcc or long long.

jacob
Nov 14 '05 #6
jacob navia wrote:
Sorry Keith but how would you implement long long in C89???

I mean, after having implemented it in lcc-win32 this is not
really easy :-)
Just because it's not easy for you doesn't mean it hasn't been done by
others.
If he hasn't long long then he is stuck. But he was speaking of
*nix system, and in most of them gcc is available,
He also said it needed to run on Windows which has a long long int
(they actually call it __int64 or something like that) but does not
support the "lld" format specifier. Windows does not support C99 and
doesn't seem to have any plans to do so.
so I think your
fears aren't sustained by any real data. It would be surprising to find a unix system without gcc or long long.
Just because gcc is available (which it very well might not be) doesn't
mean that the developer will have the luxury of using it (he indicated
that the machine was client provided).
jacob


Nov 14 '05 #7
On Mon, 21 Mar 2005 12:27:08 -0500, David Resnick
<dr******@N.o.S.p.A.m.p.l.e.a.s.e.alum.mit.edu> wrote:


You can do the following if you need to for all platforms you support.
Details for any specific platform are off topic here, but here
is an example:

#if defined(_WIN32) /* Or some other MSVC specific define */
#define my_longlong _int64
#define PRINTF_LL "%I64d"
#else
#define my_longlong long long int
#define PRINTF_LL "%lld"
#endif

my_longlong iVal;
sprintf(cTemp, "Wrong length " PRINTF_LL "\n", iVal);

-David

Thanks David & Others

- I do have a few #ifdef for long long / _int64 specific bits in my
code. Thanks for the format codes

As for what version of C their version of *nix supports, they are
pretty up to date so if I can compile it under whatever linux or unix
or win32 platforms I can find I'll be satisfied.
Nov 14 '05 #8
"tigervamp" <rg*******@gmail.com> wrote:
jacob navia wrote:
Sorry Keith but how would you implement long long in C89???

I mean, after having implemented it in lcc-win32 this is not
really easy :-)


Just because it's not easy for you doesn't mean it hasn't been done by
others.


*Smothers self*
If he hasn't long long then he is stuck. But he was speaking of
*nix system, and in most of them gcc is available,


He also said it needed to run on Windows which has a long long int
(they actually call it __int64 or something like that) but does not
support the "lld" format specifier. Windows does not support C99 and
doesn't seem to have any plans to do so.


YM Microsoft doesn't support C99. This need not prevent other
implementors from providing C99 support on MS Windows. I haven't tried
Pelles C extensively, but its help file does describe C99, so it
probably does support a good sized percentage at least.

Richard
Nov 14 '05 #9

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

Similar topics

4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
5
by: Gizmo | last post by:
Im not sure if this is a c or c++ question so I apologise if im in the wrong place. I want to convert a char* to a long. However the number that I want to convert appears to be one...
4
by: jagmeena | last post by:
Hello, I am sure this problem has been addressed before, however, I could'nt get a suitable solution to my problem. Hence I am posting here. Thanks a lot for all your help. The code I have is ...
8
by: Ramiro Barbosa, Jr. | last post by:
All, Any ideas on how to convert the first 8 bytes of raw uninterpreted sequence of bytes from 'char array;' (populated with _binary_ data read from a socket), into a 'long id'? Thank you! ...
3
by: Golan | last post by:
Hello, I have a hexa file which I need to convert to decimal. I use memcpy into variables (char for one octet, short for 2 octets and int for 4 octets) and then print the value into the file by...
18
by: No Such Luck | last post by:
Hi all: I have an unsigned char array (size 4): unsigned char array; array = 0x00; array = 0x00; array = 0x02; array = 0xe7;
11
by: TomServo | last post by:
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....
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...
2
by: Andreas Vinther | last post by:
I need to re-structure some code in a program I wrote som time ago I'd like to reuse my code instead of having to rewrite the lot. That leads me to my question. I have the following two...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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
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...

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.