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

int to *char


Hello,

How can I convert an int to a *char (best way please) I know a few round
about methods myself :)

Thank you
Jul 19 '05 #1
12 26709
"Nitin Manoharan" <nm******@bacon.math.uwaterloo.ca> wrote in message
news:Pi************************************@bacon. math.uwaterloo.ca...

Hello,

How can I convert an int to a *char (best way please) I know a few round
about methods myself :)


Have you tried reinterpret_cast<char*>(an_int) or (char*)an_int?

DW

Jul 19 '05 #2

"David White" <no.email@provided> wrote in message
news:wx******************@nasal.pacific.net.au...
"Nitin Manoharan" <nm******@bacon.math.uwaterloo.ca> wrote in message
news:Pi************************************@bacon. math.uwaterloo.ca...

Hello,

How can I convert an int to a *char (best way please) I know a few round
about methods myself :)


Have you tried reinterpret_cast<char*>(an_int) or (char*)an_int?

DW


1. Ouch. reinterpret_cast can really hurt you. It's like going through a
void* (in fact, I'll bet that's how it's implemented). I believe that
reinterpret_cast<char*>(an_int) will give you the address represented by
that integer. For example:

int a = 4;
char* chr = reinterpret_cast<char*>(a);
/*IIRC, this should be the address 0x00000004 (not at all valid).*/
2. (char*)an_int, IIRC, is not legal. You must go through valid conversions:
&((char*)an_int), which I believe can be implicit by doing &an_int
(automatically converted to char*)
Jul 19 '05 #3
"MiniDisc_2k2" <Ma******@nospam.com> wrote in message
news:DB*****************@news2.east.cox.net...

"David White" <no.email@provided> wrote in message
news:wx******************@nasal.pacific.net.au...
"Nitin Manoharan" <nm******@bacon.math.uwaterloo.ca> wrote in message
news:Pi************************************@bacon. math.uwaterloo.ca...

Hello,

How can I convert an int to a *char (best way please) I know a few round about methods myself :)
Have you tried reinterpret_cast<char*>(an_int) or (char*)an_int?

DW


1. Ouch. reinterpret_cast can really hurt you. It's like going through a
void* (in fact, I'll bet that's how it's implemented). I believe that
reinterpret_cast<char*>(an_int) will give you the address represented by
that integer. For example:

int a = 4;
char* chr = reinterpret_cast<char*>(a);
/*IIRC, this should be the address 0x00000004 (not at all valid).*/


Yes, I know it's nasty, but if that's what the OP wants to do...
2. (char*)an_int, IIRC, is not legal. You must go through valid conversions: &((char*)an_int), which I believe can be implicit by doing &an_int
(automatically converted to char*)


According to The D & E of C++, reinterpret_cast was intended as a notation
to replace the old-style cast. I thought that old-style casts could do
anything that reinterpret_cast can do, and possibly more.

My compiler happily accepts (char*)an_int.

DW

Jul 19 '05 #4
>>>Hello,

How can I convert an int to a *char (best way please) I know a few round
about methods myself :)
Have you tried reinterpret_cast<char*>(an_int) or (char*)an_int?

DW


1. Ouch. reinterpret_cast can really hurt you. It's like going through a
void* (in fact, I'll bet that's how it's implemented). I believe that
reinterpret_cast<char*>(an_int) will give you the address represented by
that integer. For example:
int a = 4;
char* chr = reinterpret_cast<char*>(a);
/*IIRC, this should be the address 0x00000004 (not at all valid).*/


It's most certainly not implemented by going through an intermediate
void *. That doesn't make any sense.
2. (char*)an_int, IIRC, is not legal. You must go through valid conversions:
&((char*)an_int), which I believe can be implicit by doing &an_int
(automatically converted to char*)


The old-style cast will do exactly the same as reinterpret_cast in this
case.

Both are legal, and both convert a pointer-to-character into an integer.
Provided that an int has at least as many bits as a pointer-to-char,
there is a guarantee that casting back to pointer-to-char will give a
usable pointer. (p 130, The C++ Programming Language, Bjarne Stroustrup
(3rd edition).)

Jul 19 '05 #5
use sprintf
"Nitin Manoharan" <nm******@bacon.math.uwaterloo.ca> wrote in message
news:Pi************************************@bacon. math.uwaterloo.ca...

Hello,

How can I convert an int to a *char (best way please) I know a few round
about methods myself :)

Thank you

Jul 19 '05 #6

"Nitin Manoharan" <nm******@bacon.math.uwaterloo.ca> wrote in message
news:Pi************************************@bacon. math.uwaterloo.ca...

Hello,

How can I convert an int to a *char (best way please) I know a few round
about methods myself :)

Thank you


There no such thing as a *char. Probably you mean char*.

Even with this correction your question can be interpreted in at least three
different ways as you can see from the answers you got. If you post one of
the ways you are doing this already then everyone will know what you are
talking about.

FWIW my answer would be use sprintf

int an_int = ...;
char an_array[99];
sprintf(an_array, "%d", an_int);

john

Jul 19 '05 #7
>
/* I'm not sure if this is standard (some people have been saying it's not, that they couldn't find the function) */

int a;
// fill a with the number here
char* chr = new char[21]; // size to at least 1 more than you need it
itoa(a, chr, 10); // 10 means decimal format, 2 would
mean binary, etc.


Most certainly is not standard.

john
Jul 19 '05 #8
David White wrote:
"David White" <no.email@provided> wrote in message
news:DV******************@nasal.pacific.net.au...
According to The D & E of C++, reinterpret_cast was intended as a
notation to replace the old-style cast.


I should have added the qualification: for conversions that "are
inherently unsafe and often implentation dependent" and return "a
value that is a crude reinterpretation of its argument".


It is my understanding that the purpose of static_cast and reinterpret_cast
was to *prevent* the C-style cast from acting like a reinterpret_cast.

For instance, say we have a class A, and a class B : A, and A *pa and B *pb;

pa = (A*)pb; // up-cast B* to A*

This has the desired result. However, if we later change the type of pb or
pa, or change B so it doesn't inherit from A anymore, the above code still
compiles and runs but has a completely undesirable effect (it would do the
same as a reinterpret_cast now). Had we written:

pa = static_cast<A*>(pb);

The code wouldn't have compiled anymore after such changes.

(and of course for downcasts dynamic_cast is even better)

If that wasn't (part of) the rationale behind static_cast, it's a bloody
nice side-effect.

--
Unforgiven

"Earth. It exists only in a corner of my memory."
Lord Dornkirk
The Vision of Escaflowne

Jul 19 '05 #9
"John Harrison" <jo*************@hotmail.com> wrote in message news:<be************@ID-196037.news.dfncis.de>...
"Nitin Manoharan" <nm******@bacon.math.uwaterloo.ca> wrote in message
news:Pi************************************@bacon. math.uwaterloo.ca...

Hello,

How can I convert an int to a *char (best way please) I know a few round
about methods myself :)

Thank you


There no such thing as a *char. Probably you mean char*.

Even with this correction your question can be interpreted in at least three
different ways as you can see from the answers you got. If you post one of
the ways you are doing this already then everyone will know what you are
talking about.

FWIW my answer would be use sprintf

int an_int = ...;
char an_array[99];
sprintf(an_array, "%d", an_int);


char[99] is a bit overkill; even 64 bits numbers will fit in 22 positions
( 10^19 < 2^64 < 10^20, plus room for sign & \0 )

In general you want to ensure that the buffer is large enough, which means
<stringstream> or snprintf

I could write a long article about that, but that would mostly be a rehash
of http://www.gotw.ca/publications/mill19.htm (by Herb Sutter&friends)

HTH,
--
Michiel Salters
Jul 19 '05 #10
Unforgiven wrote:
For instance, say we have a class A, and a class B : A, and A *pa and
B *pb;

pa = (A*)pb; // up-cast B* to A*

This has the desired result. However, if we later change the type of
pb or pa, or change B so it doesn't inherit from A anymore, the above
code still compiles and runs but has a completely undesirable effect
(it would do the same as a reinterpret_cast now). Had we written:

pa = static_cast<A*>(pb);

The code wouldn't have compiled anymore after such changes.

(and of course for downcasts dynamic_cast is even better)

If that wasn't (part of) the rationale behind static_cast, it's a
bloody nice side-effect.


AFAIK, that just is the rationale. The idea is to have more fine-grained
control about the actual conversion. A C style cast does the same as
any combination of static_cast, const_cast and reinterpret_cast that
would be needed for the specific combination of types. So you can also
easily cast away constness with it by accident.

Jul 19 '05 #11
Dhruv wrote:
On Sun, 06 Jul 2003 07:12:39 +0100, John Harrison wrote:

/* I'm not sure if this is standard (some people have been saying
it's

not,
that they couldn't find the function) */

int a;
// fill a with the number here
char* chr = new char[21]; // size to at least 1 more than you
need it
itoa(a, chr, 10); // 10 means decimal format, 2
would mean binary, etc.


Most certainly is not standard.


Whoa!!! I spent sooooo much time searching each header to find out
where this function is. I had practically #included the whole C
library, to find it, but could not just to find out that it's
non-stadard, and that's why it wasn't there. No wonder, the glibc
documentation did not have any information on it.

-Dhruv.

ps: Is there any place I can check whether a function is standard or
not?


The standard? :-)
Also if you're on a un*x like operating system, the man page of the
function should tell you if that function belongs to any standard. On
other systems, there is probably similar information in the
compiler/library documentation.

Jul 19 '05 #12
On Mon, 07 Jul 2003 22:36:06 +0200, Rolf Magnus wrote:

[snip]......

The standard? :-)
Also if you're on a un*x like operating system, the man page of the
function should tell you if that function belongs to any standard. On
other systems, there is probably similar information in the
compiler/library documentation.


Yes :-) But, most of the times, the documentaion on Linux is pretty poor
with this Red Hat distro. that I have, that I've come not to trust it any
more for checking if a function exists or not. I should get a C standard
describibg the functions in the C standard library. RIght now I'm usign
the glibc documentation (postscript) for checking the existence of a
function.

-Dhruv.
Jul 19 '05 #13

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

Similar topics

9
by: Christopher Benson-Manica | last post by:
I need a smart char * class, that acts like a char * in all cases, but lets you do some std::string-type stuff with it. (Please don't say to use std::string - it's not an option...). This is my...
5
by: Alex Vinokur | last post by:
"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message news:4180f756.197032434@news.individual.net... to news:comp.lang.c > ben19777@hotmail.com (Ben) wrote: > > 2) Structure casted into an...
5
by: Sona | last post by:
I understand the problem I'm having but am not sure how to fix it. My code passes two char* to a function which reads in some strings from a file and copies the contents into the two char*s. Now...
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...
5
by: jab3 | last post by:
(again :)) Hello everyone. I'll ask this even at risk of being accused of not researching adequately. My question (before longer reasoning) is: How does declaring (or defining, whatever) a...
12
by: GRoll35 | last post by:
I get 4 of those errors. in the same spot. I'll show my parent class, child class, and my driver. All that is suppose to happen is the user enters data and it uses parent/child class to display...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
4
by: Paul Brettschneider | last post by:
Hello all, consider the following code: typedef char T; class test { T *data; public: void f(T, T, T); void f2(T, T, T);
16
by: s0suk3 | last post by:
This code #include <stdio.h> int main(void) { int hello = {'h', 'e', 'l', 'l', 'o'}; char *p = (void *) hello; for (size_t i = 0; i < sizeof(hello); ++i) {
29
by: Kenzogio | last post by:
Hi, I have a struct "allmsg" and him member : unsigned char card_number; //16 allmsg.card_number
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
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
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
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
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...

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.