int to *char

  • Thread starter Nitin Manoharan
  • Start date
N

Nitin Manoharan

Hello,

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

Thank you
 
B

Buster Copley

Hello,
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).)
 
D

David White

Nitin Manoharan said:
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
 
M

MiniDisc_2k2

David White said:
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*)
 
D

David White

MiniDisc_2k2 said:
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
 
J

John Harrison

Nitin Manoharan said:
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
 
J

John Harrison

/* 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
 
U

Unforgiven

David said:
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
 
M

Michiel Salters

John Harrison said:
Nitin Manoharan said:
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,
 
R

Rolf Magnus

Unforgiven said:
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.
 
R

Rolf Magnus

Dhruv said:
/* 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.
 
D

Dhruv

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top