CString to char array convertion (vice versa)

Discussion in 'C++' started by Tim Wong, Jan 20, 2005.

  1. Tim  Wong

    Tim Wong Guest

    All:

    I am trying to convert a CString value to an unsigned char array.
    I found some code online that will allow me to compile, but when I try
    to print out...i get a whole mess.

    /*Begin Code*/
    CString day("01");
    unsigned char testDay[2];

    //Code snippet found online
    strncpy((char *) testDay, (LPCTSTR) day, sizeof(testDay));

    printf(day);

    /*End Code*/

    Up there i am trying to "cast" the Cstring into an unsigned char[2].
    Any suggestions? What if it were the other way around...how would you
    go about doing that?
     
    Tim Wong, Jan 20, 2005
    #1
    1. Advertising

  2. Tim  Wong

    Howard Guest

    "Tim Wong" <> wrote in message
    news:...
    > All:
    >
    > I am trying to convert a CString value to an unsigned char array.
    > I found some code online that will allow me to compile, but when I try
    > to print out...i get a whole mess.
    >
    > /*Begin Code*/
    > CString day("01");
    > unsigned char testDay[2];
    >
    > //Code snippet found online
    > strncpy((char *) testDay, (LPCTSTR) day, sizeof(testDay));
    >
    > printf(day);
    >
    > /*End Code*/
    >
    > Up there i am trying to "cast" the Cstring into an unsigned char[2].
    > Any suggestions? What if it were the other way around...how would you
    > go about doing that?
    >


    You'd be better off asking in a VC++ or Windows newsgroup, I think.

    But I can see at least one problem right away. You're casting the CString
    (an object) as a pointer (LPCTSTR, which, if I recall, is a "long pointer to
    constant T-string"). That is simply not a valid cast. There should be a
    member function or variable inside CString that allows you to access its
    internal string data. Read your doc's or your on-line manual, or ask in a
    newsgroup that knows about CString.

    -Howard
     
    Howard, Jan 20, 2005
    #2
    1. Advertising

  3. Tim  Wong

    Mike Wahler Guest

    "Tim Wong" <> wrote in message
    news:...
    > All:
    >
    > I am trying to convert a CString value to an unsigned char array.
    > I found some code online that will allow me to compile, but when I try
    > to print out...i get a whole mess.
    >
    > /*Begin Code*/
    > CString day("01");
    > unsigned char testDay[2];
    >
    > //Code snippet found online
    > strncpy((char *) testDay, (LPCTSTR) day, sizeof(testDay));
    >
    > printf(day);
    >
    > /*End Code*/
    >
    > Up there i am trying to "cast" the Cstring into an unsigned char[2].
    > Any suggestions? What if it were the other way around...how would you
    > go about doing that?


    Ask about all that MFC and Windows stuff in an appropriate
    newsgroup:

    #include <iostream>
    #include <string>

    int main()
    {
    std::string day("01");
    std::cout << day << '\n';
    return 0;
    }

    Look, Ma, no arrays, no C library functions, no MFC
    (nor is any of that needed).

    -Mike
     
    Mike Wahler, Jan 20, 2005
    #3
  4. Tim  Wong

    Jeff Flinn Guest

    Howard wrote:
    > "Tim Wong" <> wrote in message
    > news:...
    >> All:
    >>
    >> I am trying to convert a CString value to an unsigned char array.


    Why do you need an unsigned char array?

    >> I found some code online that will allow me to compile, but when I
    >> try to print out...i get a whole mess.
    >>
    >> /*Begin Code*/
    >> CString day("01");
    >> unsigned char testDay[2];



    You didn't leave room for the trailing '/0'.

    >>
    >> //Code snippet found online
    >> strncpy((char *) testDay, (LPCTSTR) day, sizeof(testDay));



    I don't use strncpy, is testDay the destination? Also if this is a unicode
    build you

    >>
    >> printf(day);


    Did you mean testDay?

    >>
    >> /*End Code*/
    >>
    >> Up there i am trying to "cast" the Cstring into an unsigned char[2].
    >> Any suggestions? What if it were the other way around...how would
    >> you go about doing that?
    >>

    >
    > You'd be better off asking in a VC++ or Windows newsgroup, I think.
    >
    > But I can see at least one problem right away. You're casting the
    > CString (an object) as a pointer (LPCTSTR, which, if I recall, is a
    > "long pointer to constant T-string"). That is simply not a valid
    > cast. There should be a member function or variable inside CString
    > that allows you to access its internal string data.


    In this case CString::eek:perator (LPCTSTR)()const; is that member function.

    Jeff
     
    Jeff Flinn, Jan 20, 2005
    #4
  5. Tim  Wong

    Howard Guest

    "Jeff Flinn" <> wrote in message
    news:csp7s4$mds$...
    > Howard wrote:
    >> "Tim Wong" <> wrote in message
    >> news:...
    >>> All:
    >>>


    >>> //Code snippet found online
    >>> strncpy((char *) testDay, (LPCTSTR) day, sizeof(testDay));


    >
    >> But I can see at least one problem right away. You're casting the
    >> CString (an object) as a pointer (LPCTSTR, which, if I recall, is a
    >> "long pointer to constant T-string"). That is simply not a valid
    >> cast. There should be a member function or variable inside CString
    >> that allows you to access its internal string data.


    >
    > In this case CString::eek:perator (LPCTSTR)()const; is that member function.
    >
    > Jeff


    Cool. I didn't know that. (Which is another good reason VC++-specifc
    questions belong in a newsgroup where they KNOW such VC++ things!)

    But... since that's the case, why the cast? A conversion operator returns a
    value of the type specified, so no cast is needed. You should be able to
    just use the CString variable wherever an LPCTSTR is called for (as is
    confirmed by a perusal of the online help).

    -Howard
     
    Howard, Jan 20, 2005
    #5
  6. Tim  Wong

    Tim Wong Guest

    My apologies for posting this in the incorrect group (still new to this
    newsgroup posting).

    It turns out the code I initially wrote is correct.

    The casting actually takes the string and puts it into "testDay"
    without the null terminator. Because of this, it looks funny when it
    prints out. I knew that there was no null terminator, but did not
    realize it would print out in a wierd way.

    Sorry for the hastle. Thanks for the replys

    Howard wrote:
    > "Jeff Flinn" <> wrote in message
    > news:csp7s4$mds$...
    > > Howard wrote:
    > >> "Tim Wong" <> wrote in message
    > >> news:...
    > >>> All:
    > >>>

    >
    > >>> //Code snippet found online
    > >>> strncpy((char *) testDay, (LPCTSTR) day, sizeof(testDay));

    >
    > >
    > >> But I can see at least one problem right away. You're casting the
    > >> CString (an object) as a pointer (LPCTSTR, which, if I recall, is

    a
    > >> "long pointer to constant T-string"). That is simply not a valid
    > >> cast. There should be a member function or variable inside

    CString
    > >> that allows you to access its internal string data.

    >
    > >
    > > In this case CString::eek:perator (LPCTSTR)()const; is that member

    function.
    > >
    > > Jeff

    >
    > Cool. I didn't know that. (Which is another good reason

    VC++-specifc
    > questions belong in a newsgroup where they KNOW such VC++ things!)
    >
    > But... since that's the case, why the cast? A conversion operator

    returns a
    > value of the type specified, so no cast is needed. You should be

    able to
    > just use the CString variable wherever an LPCTSTR is called for (as

    is
    > confirmed by a perusal of the online help).
    >
    > -Howard
     
    Tim Wong, Jan 21, 2005
    #6
    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. Replies:
    3
    Views:
    8,862
  2. Navaneeth
    Replies:
    8
    Views:
    704
    Keith Thompson
    Jan 1, 2011
  3. Navaneeth
    Replies:
    3
    Views:
    338
    Seebs
    Jan 1, 2011
  4. Navaneeth
    Replies:
    1
    Views:
    335
    Ben Bacarisse
    Jan 4, 2011
  5. Navaneeth
    Replies:
    3
    Views:
    346
    Thad Smith
    Jan 5, 2011
Loading...

Share This Page