473,397 Members | 2,099 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.

int to char* conversion

Hi,

What is the simplest way of converting an int into a char*? I realized type
casting did not work for some reason. Anyway, I'm looking for just a single
line statement, which can do this.
Regards
weeeeee
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.510 / Virus Database: 307 - Release Date: 14/08/2003
Jul 19 '05 #1
2 26399
Ying Yang wrote:
Hi,

What is the simplest way of converting an int into a char*? I realized type
casting did not work for some reason.
Since there is no implicit conversion, type casting is the only way.
Anyway, I'm looking for just a single
line statement, which can do this.


char *cp = reinterpret_cast<char *>(some_int);

However, this is usually not safe.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2
Ying Yang wrote:
Hi,

What is the simplest way of converting an int into a char*? I realized type
casting did not work for some reason. Anyway, I'm looking for just a single
line statement, which can do this.


OK, because I'm bored, here's a real answer to the question I *think*
you meant to ask. In the future, you might want to try to state your
question more clearly.

The recommended way (i.e., don't use char *s at all - use std::strings
instead):

#include <sstream>
#include <string>

int main()
{
int some_int = 3028;
std::ostringstream sout;
sout << some_int;

std::string converted_string(sout.str());
}

The less recommended way (type safe, copying into a dynamically sized
array):

#include <sstream>
#include <cstring>

int main()
{
int some_int = 3028;
std::ostringstream sout;
sout << some_int;

char *buff = new char[sout.str().length() + 1];
std::strcpy(buff, sout.str().c_str());
// ... (use buff here)
delete [] buff;
}

The extremely unrecommended way (no type safety, writing into a
carefully sized array):

#include <climits>
#include <cstdio>

int main()
{
int some_int = 3028;
char array[(sizeof(int) * CHAR_BIT + 2) / 3 + 1 + 1];

std::sprintf(array, "%d", some_int);
}

See http://www.eskimo.com/~scs/C-faq/q12.21.html for an explanation of
the size used for array.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3

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

Similar topics

30
by: Tim Johansson | last post by:
I'm new to C++, and tried to start making a script that will shuffle an array. Can someone please tell me what's wrong? #include <iostream.h> #include <string.h> int main () {...
2
by: Peter Nilsson | last post by:
In a post regarding toupper(), Richard Heathfield once asked me to think about what the conversion of a char to unsigned char would mean, and whether it was sensible to actually do so. And pete has...
10
by: kevin.hall | last post by:
GCC 3.3 and MSVS 6.0 have no problem converting char* to const char* (not even a warning), but MS's WinCE compiler generated an error complained that this was not possible. MS's WinCE compiler did...
4
by: jim_geissman | last post by:
I have data tables that include ZIP code, as char(5). The values look like integers, but they are padded with leading zeroes to fill out 5 characters, '00234'. There are SPs to look up data,...
1
by: lovecreatesbeauty | last post by:
There is a warning/(error? I remember it is an error for line 10 on some compilers before. At least on g++, it is an error.) for line 10. I first read a similar example from `Expert C Programming...
33
by: Mark P | last post by:
A colleague asked me something along the lines of the following today. For some type X he has: X* px = new X; Then he wants to convert px to a char* (I'm guessing for the purpose of...
3
by: Kevin Frey | last post by:
I am porting Managed C++ code from VS2003 to VS2005. Therefore adopting the new C++/CLI syntax rather than /clr:oldSyntax. Much of our managed code is concerned with interfacing to native C++...
3
by: utab | last post by:
Dear all, I was trying to write a more complex program, and while searching for sth in my reference C++ primer, by Lippman. I had a question in the mind, see the code below #include <string>...
8
by: AGRAJA | last post by:
how to convert unsigned to char? Ref: http://www.thescripts.com/forum/thread477545.html how do I print without the leading ffffff (yet the result should be char*)?
0
by: maheshmohta | last post by:
Background Often while remodeling legacy application, one of the important tasks for the architects is to have an optimum usage of storage capabilities of database. Most of the legacy applications...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
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.