Connecting Tech Pros Worldwide Help | Site Map

int to *char

Nitin Manoharan
Guest
 
Posts: n/a
#1: Jul 19 '05

Hello,

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

Thank you


David White
Guest
 
Posts: n/a
#2: Jul 19 '05

re: int to *char


"Nitin Manoharan" <nmanohar@bacon.math.uwaterloo.ca> wrote in message
news:Pine.SOL.4.44.0307052001320.562-100000@bacon.math.uwaterloo.ca...[color=blue]
>
> Hello,
>
> How can I convert an int to a *char (best way please) I know a few round
> about methods myself :)[/color]

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

DW



MiniDisc_2k2
Guest
 
Posts: n/a
#3: Jul 19 '05

re: int to *char



"David White" <no.email@provided> wrote in message
news:wxKNa.8645$eE.115038@nasal.pacific.net.au...[color=blue]
> "Nitin Manoharan" <nmanohar@bacon.math.uwaterloo.ca> wrote in message
> news:Pine.SOL.4.44.0307052001320.562-100000@bacon.math.uwaterloo.ca...[color=green]
> >
> > Hello,
> >
> > How can I convert an int to a *char (best way please) I know a few round
> > about methods myself :)[/color]
>
> Have you tried reinterpret_cast<char*>(an_int) or (char*)an_int?
>
> DW
>
>
>[/color]

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*)


David White
Guest
 
Posts: n/a
#4: Jul 19 '05

re: int to *char


"MiniDisc_2k2" <MattDelB@nospam.com> wrote in message
news:DBKNa.7700$ZT5.2971@news2.east.cox.net...[color=blue]
>
> "David White" <no.email@provided> wrote in message
> news:wxKNa.8645$eE.115038@nasal.pacific.net.au...[color=green]
> > "Nitin Manoharan" <nmanohar@bacon.math.uwaterloo.ca> wrote in message
> > news:Pine.SOL.4.44.0307052001320.562-100000@bacon.math.uwaterloo.ca...[color=darkred]
> > >
> > > Hello,
> > >
> > > How can I convert an int to a *char (best way please) I know a few[/color][/color][/color]
round[color=blue][color=green][color=darkred]
> > > about methods myself :)[/color]
> >
> > Have you tried reinterpret_cast<char*>(an_int) or (char*)an_int?
> >
> > DW
> >
> >
> >[/color]
>
> 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).*/[/color]

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

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



Buster Copley
Guest
 
Posts: n/a
#5: Jul 19 '05

re: int to *char


>>>Hello,[color=blue][color=green][color=darkred]
>>>
>>>How can I convert an int to a *char (best way please) I know a few round
>>>about methods myself :)[/color]
>>
>>Have you tried reinterpret_cast<char*>(an_int) or (char*)an_int?
>>
>>DW[/color]
>
> 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).*/[/color]

It's most certainly not implemented by going through an intermediate
void *. That doesn't make any sense.
[color=blue]
> 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*)
>[/color]

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).)

lredmond
Guest
 
Posts: n/a
#6: Jul 19 '05

re: int to *char


use sprintf
"Nitin Manoharan" <nmanohar@bacon.math.uwaterloo.ca> wrote in message
news:Pine.SOL.4.44.0307052001320.562-100000@bacon.math.uwaterloo.ca...[color=blue]
>
> Hello,
>
> How can I convert an int to a *char (best way please) I know a few round
> about methods myself :)
>
> Thank you
>
>[/color]


John Harrison
Guest
 
Posts: n/a
#7: Jul 19 '05

re: int to *char



"Nitin Manoharan" <nmanohar@bacon.math.uwaterloo.ca> wrote in message
news:Pine.SOL.4.44.0307052001320.562-100000@bacon.math.uwaterloo.ca...[color=blue]
>
> Hello,
>
> How can I convert an int to a *char (best way please) I know a few round
> about methods myself :)
>
> Thank you
>[/color]

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



John Harrison
Guest
 
Posts: n/a
#8: Jul 19 '05

re: int to *char


>[color=blue]
> /* I'm not sure if this is standard (some people have been saying it's[/color]
not,[color=blue]
> 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.
>[/color]

Most certainly is not standard.

john


Unforgiven
Guest
 
Posts: n/a
#9: Jul 19 '05

re: int to *char


David White wrote:[color=blue]
> "David White" <no.email@provided> wrote in message
> news:DVKNa.8646$eE.115218@nasal.pacific.net.au...[color=green]
>> According to The D & E of C++, reinterpret_cast was intended as a
>> notation to replace the old-style cast.[/color]
>
> 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".[/color]

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

Michiel Salters
Guest
 
Posts: n/a
#10: Jul 19 '05

re: int to *char


"John Harrison" <john_andronicus@hotmail.com> wrote in message news:<be8ej9$29j16$1@ID-196037.news.dfncis.de>...[color=blue]
> "Nitin Manoharan" <nmanohar@bacon.math.uwaterloo.ca> wrote in message
> news:Pine.SOL.4.44.0307052001320.562-100000@bacon.math.uwaterloo.ca...[color=green]
> >
> > Hello,
> >
> > How can I convert an int to a *char (best way please) I know a few round
> > about methods myself :)
> >
> > Thank you
> >[/color]
>
> 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);[/color]

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
Rolf Magnus
Guest
 
Posts: n/a
#11: Jul 19 '05

re: int to *char


Unforgiven wrote:
[color=blue]
> 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.[/color]

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.

Rolf Magnus
Guest
 
Posts: n/a
#12: Jul 19 '05

re: int to *char


Dhruv wrote:
[color=blue]
> On Sun, 06 Jul 2003 07:12:39 +0100, John Harrison wrote:
>[color=green][color=darkred]
>>>
>>> /* I'm not sure if this is standard (some people have been saying
>>> it's[/color]
>> not,[color=darkred]
>>> 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.
>>>[/color]
>>
>> Most certainly is not standard.
>>[/color]
>
> 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?[/color]

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.

Dhruv
Guest
 
Posts: n/a
#13: Jul 19 '05

re: int to *char


On Mon, 07 Jul 2003 22:36:06 +0200, Rolf Magnus wrote:

[snip]......
[color=blue]
>
> 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.[/color]

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.


Closed Thread


Similar C / C++ bytes