int to *char

Discussion in 'C++' started by Nitin Manoharan, Jul 6, 2003.

  1. Hello,

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

    Thank you
    Nitin Manoharan, Jul 6, 2003
    #1
    1. Advertising

  2. >>>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).)
    Buster Copley, Jul 6, 2003
    #2
    1. Advertising

  3. Nitin Manoharan

    David White Guest

    "Nitin Manoharan" <> wrote in message
    news:p...
    >
    > 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
    David White, Jul 6, 2003
    #3
  4. Nitin Manoharan

    MiniDisc_2k2 Guest

    "David White" <no.email@provided> wrote in message
    news:wxKNa.8645$...
    > "Nitin Manoharan" <> wrote in message
    > news:p...
    > >
    > > 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*)
    MiniDisc_2k2, Jul 6, 2003
    #4
  5. Nitin Manoharan

    David White Guest

    "MiniDisc_2k2" <> wrote in message
    news:DBKNa.7700$...
    >
    > "David White" <no.email@provided> wrote in message
    > news:wxKNa.8645$...
    > > "Nitin Manoharan" <> wrote in message
    > > news:p...
    > > >
    > > > 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
    David White, Jul 6, 2003
    #5
  6. Nitin Manoharan

    lredmond Guest

    use sprintf
    "Nitin Manoharan" <> wrote in message
    news:p...
    >
    > Hello,
    >
    > How can I convert an int to a *char (best way please) I know a few round
    > about methods myself :)
    >
    > Thank you
    >
    >
    lredmond, Jul 6, 2003
    #6
  7. "Nitin Manoharan" <> wrote in message
    news:p...
    >
    > 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
    John Harrison, Jul 6, 2003
    #7
  8. >
    > /* 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
    John Harrison, Jul 6, 2003
    #8
  9. Nitin Manoharan

    Unforgiven Guest

    David White wrote:
    > "David White" <no.email@provided> wrote in message
    > news:DVKNa.8646$...
    >> 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
    Unforgiven, Jul 6, 2003
    #9
  10. "John Harrison" <> wrote in message news:<be8ej9$29j16$>...
    > "Nitin Manoharan" <> wrote in message
    > news:p...
    > >
    > > 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
    Michiel Salters, Jul 7, 2003
    #10
  11. Nitin Manoharan

    Rolf Magnus Guest

    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.
    Rolf Magnus, Jul 7, 2003
    #11
  12. Nitin Manoharan

    Rolf Magnus Guest

    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.
    Rolf Magnus, Jul 7, 2003
    #12
  13. Nitin Manoharan

    Dhruv Guest

    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.
    Dhruv, Jul 18, 2003
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,213
    Martien Verbruggen
    Jun 27, 2003
  2. trey

    newbie: char* int and char *int

    trey, Sep 10, 2003, in forum: C Programming
    Replies:
    7
    Views:
    404
    Irrwahn Grausewitz
    Sep 10, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,634
    Old Wolf
    Jan 20, 2004
  4. lovecreatesbeauty
    Replies:
    1
    Views:
    1,046
    Ian Collins
    May 9, 2006
  5. gert
    Replies:
    20
    Views:
    1,163
Loading...

Share This Page