"Nitin Manoharan" <nmanohar@bacon .math.uwaterloo .ca> wrote in message
news:Pine.SOL.4 .44.03070520013 20.562-100000@bacon.ma th.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_cas t<char*>(an_int ) or (char*)an_int?
"David White" <no.email@provi ded> wrote in message
news:wxKNa.8645 $eE.115038@nasa l.pacific.net.a u...[color=blue]
> "Nitin Manoharan" <nmanohar@bacon .math.uwaterloo .ca> wrote in message
> news:Pine.SOL.4 .44.03070520013 20.562-100000@bacon.ma th.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_cas t<char*>(an_int ) or (char*)an_int?
>
> DW
>
>
>[/color]
1. Ouch. reinterpret_cas t 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_cas t<char*>(an_int ) will give you the address represented by
that integer. For example:
int a = 4;
char* chr = reinterpret_cas t<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*)
"MiniDisc_2 k2" <MattDelB@nospa m.com> wrote in message
news:DBKNa.7700 $ZT5.2971@news2 .east.cox.net.. .[color=blue]
>
> "David White" <no.email@provi ded> wrote in message
> news:wxKNa.8645 $eE.115038@nasa l.pacific.net.a u...[color=green]
> > "Nitin Manoharan" <nmanohar@bacon .math.uwaterloo .ca> wrote in message
> > news:Pine.SOL.4 .44.03070520013 20.562-100000@bacon.ma th.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_cas t<char*>(an_int ) or (char*)an_int?
> >
> > DW
> >
> >
> >[/color]
>
> 1. Ouch. reinterpret_cas t 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_cas t<char*>(an_int ) will give you the address represented by
> that integer. For example:
>
> int a = 4;
> char* chr = reinterpret_cas t<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_cas t was intended as a notation
to replace the old-style cast. I thought that old-style casts could do
anything that reinterpret_cas t can do, and possibly more.
>>>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_cas t<char*>(an_int ) or (char*)an_int?
>>
>>DW[/color]
>
> 1. Ouch. reinterpret_cas t 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_cas t<char*>(an_int ) will give you the address represented by
> that integer. For example:
> int a = 4;
> char* chr = reinterpret_cas t<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_cas t 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).)
use sprintf
"Nitin Manoharan" <nmanohar@bacon .math.uwaterloo .ca> wrote in message
news:Pine.SOL.4 .44.03070520013 20.562-100000@bacon.ma th.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]
"Nitin Manoharan" <nmanohar@bacon .math.uwaterloo .ca> wrote in message
news:Pine.SOL.4 .44.03070520013 20.562-100000@bacon.ma th.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_arra y, "%d", an_int);
>[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]
David White wrote:[color=blue]
> "David White" <no.email@provi ded> wrote in message
> news:DVKNa.8646 $eE.115218@nasa l.pacific.net.a u...[color=green]
>> According to The D & E of C++, reinterpret_cas t 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 reinterpretatio n of its argument".[/color]
It is my understanding that the purpose of static_cast and reinterpret_cas t
was to *prevent* the C-style cast from acting like a reinterpret_cas t.
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_cas t 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
"John Harrison" <john_andronicu s@hotmail.com> wrote in message news:<be8ej9$29 j16$1@ID-196037.news.dfn cis.de>...[color=blue]
> "Nitin Manoharan" <nmanohar@bacon .math.uwaterloo .ca> wrote in message
> news:Pine.SOL.4 .44.03070520013 20.562-100000@bacon.ma th.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_arra y, "%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
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_cas t 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_cas t that
would be needed for the specific combination of types. So you can also
easily cast away constness with it by accident.
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.
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.
Comment