question on casting

Discussion in 'C++' started by John Ratliff, Mar 14, 2006.

  1. John Ratliff

    John Ratliff Guest

    What is the proper way to cast charater pointers to and from unsigned?

    For example, say I had a heap allocated char *ptr;
    char *ptr = new char[0x2000];

    and I wanted to convert it to an unsigned char *uptr;

    unsigned char *uptr = (unsigned char *)ptr; // C-style works
    unsigned char *uptr = static_cast<unsigned char *>(ptr);
    // static_cast doesn't
    unsigned char *uptr = reinterpret_cast<unsigned char *>(ptr);
    // reinterpret_cast works, but is this correct?

    Thanks,

    --John Ratliff
    John Ratliff, Mar 14, 2006
    #1
    1. Advertising

  2. John Ratliff

    Phlip Guest

    John Ratliff wrote:

    > What is the proper way to cast charater pointers to and from unsigned?
    >
    > For example, say I had a heap allocated char *ptr;
    > char *ptr = new char[0x2000];
    >
    > and I wanted to convert it to an unsigned char *uptr;
    >
    > unsigned char *uptr = (unsigned char *)ptr; // C-style works
    > unsigned char *uptr = static_cast<unsigned char *>(ptr);
    > // static_cast doesn't
    > unsigned char *uptr = reinterpret_cast<unsigned char *>(ptr);
    > // reinterpret_cast works, but is this correct?


    You are correct to not want to use a C-style cast, and to try to use the
    cast closest to static_cast.

    With a few minor exceptions, every C-style cast has at least one equivalent
    elaborate_cast. (The exceptions are dynamic_cast, which is new, and
    const_cast, which might be needed to assist another cast to de-qualify a
    type and match a sloppy C-style cast).

    So, yes, unsigned chars are not signed chars, just as the class SimCity is
    not a std::string. So copying the bits out of one and jamming them into
    another is a reinterpretation, and reinterpret_cast is needed.

    --
    Phlip
    http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
    Phlip, Mar 14, 2006
    #2
    1. Advertising

  3. John Ratliff

    John Ratliff Guest

    Phlip wrote:
    > John Ratliff wrote:
    >
    >
    >>What is the proper way to cast charater pointers to and from unsigned?
    >>
    >>For example, say I had a heap allocated char *ptr;
    >>char *ptr = new char[0x2000];
    >>
    >>and I wanted to convert it to an unsigned char *uptr;
    >>
    >>unsigned char *uptr = (unsigned char *)ptr; // C-style works
    >>unsigned char *uptr = static_cast<unsigned char *>(ptr);
    >> // static_cast doesn't
    >>unsigned char *uptr = reinterpret_cast<unsigned char *>(ptr);
    >> // reinterpret_cast works, but is this correct?

    >
    >
    > You are correct to not want to use a C-style cast, and to try to use the
    > cast closest to static_cast.
    >
    > With a few minor exceptions, every C-style cast has at least one equivalent
    > elaborate_cast. (The exceptions are dynamic_cast, which is new, and
    > const_cast, which might be needed to assist another cast to de-qualify a
    > type and match a sloppy C-style cast).
    >
    > So, yes, unsigned chars are not signed chars, just as the class SimCity is
    > not a std::string. So copying the bits out of one and jamming them into
    > another is a reinterpretation, and reinterpret_cast is needed.
    >


    Thanks,

    --John Ratliff
    John Ratliff, Mar 14, 2006
    #3
  4. John Ratliff

    Tomás Guest

    John Ratliff posted:

    > What is the proper way to cast charater pointers to and from unsigned?
    >
    > For example, say I had a heap allocated char *ptr;
    > char *ptr = new char[0x2000];
    >
    > and I wanted to convert it to an unsigned char *uptr;
    >
    > unsigned char *uptr = (unsigned char *)ptr; // C-style works
    > unsigned char *uptr = static_cast<unsigned char *>(ptr);
    > // static_cast doesn't
    > unsigned char *uptr = reinterpret_cast<unsigned char *>(ptr);
    > // reinterpret_cast works, but is this correct?
    >
    > Thanks,
    >
    > --John Ratliff



    unsigned char* Convert(char* p)
    {
    return reinterpret_cast< unsigned char* >(p);
    }

    Here's my method, it's not very scientific but it does the trick:

    1) Try use static_cast.

    If it doesn't compile then:

    2) Try use reinterpret_cast

    If it _still_ doesn't compile, then don't hesitate to use dirty methods.


    -Tomás
    Tomás, Mar 14, 2006
    #4
    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. Remco
    Replies:
    1
    Views:
    350
    Alvin Bruney [MVP]
    Apr 11, 2004
  2. David Eynon

    casting question

    David Eynon, Jul 1, 2003, in forum: Java
    Replies:
    2
    Views:
    859
    Roedy Green
    Jul 1, 2003
  3. kevin
    Replies:
    11
    Views:
    5,798
    Andrew McDonagh
    Jan 8, 2005
  4. Wally Barnes
    Replies:
    3
    Views:
    519
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    559
    James Kanze
    Dec 20, 2009
Loading...

Share This Page