unsigned char* to CString - problem

Discussion in 'C++' started by Cactus, Nov 13, 2006.

  1. Cactus

    Cactus Guest

    How to convert unsigned char* to CString:

    I wrote some function:

    u_char_[0] =55;
    u_char_[1] =66;
    u_char_[2] =77;
    .........=ii......

    Convert_to_CS(u_char_);

    ****
    void Convert_to_CS (unsigned char* u_char){

    CString str0,str;

    str0.Format("%d",u_char[0]);
    AfxMessageBox ("First:" +str0);

    CString str(u_char);
    AfxMessageBox ("Second:" +str);

    }

    ***

    Output:
    First: A
    Second: // so nothing, why ??

    Why working only first convetion ? I want convert whole array of uchar,
    how to do it ?
    Cactus, Nov 13, 2006
    #1
    1. Advertising

  2. Cactus

    BobR Guest

    Cactus wrote in message ...
    >How to convert unsigned char* to CString:
    >I wrote some function:
    >
    >u_char_[0] =55;
    >u_char_[1] =66;
    >u_char_[2] =77;
    >........=ii......
    >Convert_to_CS(u_char_);
    >****
    >void Convert_to_CS (unsigned char* u_char){
    > CString str0,str;
    > str0.Format("%d",u_char[0]);
    > AfxMessageBox ("First:" +str0);
    > CString str(u_char);
    > AfxMessageBox ("Second:" +str);
    > }
    >***
    >Output:
    >First: A
    >Second: // so nothing, why ??
    >
    >Why working only first convetion ? I want convert whole array of uchar,
    >how to do it ?


    Show us the definition for CString, AfxMessageBox.

    http://www.parashift.com/c -faq-lite/how-to-post.html#faq-5.9).

    --
    Bob R
    POVrookie
    BobR, Nov 13, 2006
    #2
    1. Advertising

  3. Cactus

    Salt_Peter Guest

    Cactus wrote:
    > How to convert unsigned char* to CString:
    >
    > I wrote some function:
    >
    > u_char_[0] =55;
    > u_char_[1] =66;
    > u_char_[2] =77;
    > ........=ii......
    >
    > Convert_to_CS(u_char_);
    >
    > ****
    > void Convert_to_CS (unsigned char* u_char){
    >
    > CString str0,str;
    >
    > str0.Format("%d",u_char[0]);
    > AfxMessageBox ("First:" +str0);
    >
    > CString str(u_char);
    > AfxMessageBox ("Second:" +str);
    >
    > }
    >
    > ***
    >
    > Output:
    > First: A
    > Second: // so nothing, why ??
    >
    > Why working only first convetion ? I want convert whole array of uchar,
    > how to do it ?


    Why should it work for the second since you never passed it the second?

    Since you insist on a C++ solution, then consider the following:
    a) an array of unsigned char is *not* terminated.
    b) you therefore need to terminate it if you plan to pass it around.

    Here is a solution, if you need this to work with CStrings and
    AFXMessageBox, then i'ld suggest asking a relevant newsgroup since
    neither is C++.

    #include <iostream>
    #include <string>
    #include <sstream>

    template< typename T, const size_t Size >
    std::string Convert(T (&array)[Size])
    {
    std::eek:stringstream oss;
    oss << array;
    return oss.str();
    }

    int main(int argc, char* argv[])
    {
    typedef unsigned char UChar;
    UChar A[] = {55, 56, 66, 77, 0};
    for (size_t i = 0; i < sizeof(A)/sizeof(UChar); ++i)
    {
    std::cout << "A[" << i << "] = ";
    std::cout << A << std::endl;
    }

    std::cout << Convert(A) << std::endl;
    }

    /*
    A[0] = 7
    A[1] = 8
    A[2] = B
    A[3] = M
    A[4] =
    78BM
    */
    Salt_Peter, Nov 13, 2006
    #3
  4. Cactus

    Jim Langston Guest

    "Cactus" <> wrote in message
    news:...
    > How to convert unsigned char* to CString:
    >
    > I wrote some function:
    >
    > u_char_[0] =55;
    > u_char_[1] =66;
    > u_char_[2] =77;
    > ........=ii......
    >
    > Convert_to_CS(u_char_);
    >
    > ****
    > void Convert_to_CS (unsigned char* u_char){
    >
    > CString str0,str;
    >
    > str0.Format("%d",u_char[0]);
    > AfxMessageBox ("First:" +str0);
    >
    > CString str(u_char);


    CString probably doesn't have a constructor taking an unsigned char * and
    converting it to a CString (whatever that is). But it seems to be accepting
    it for compiling, but you'll have to check the documentation (for the non
    standard) CString to figure out what it's supposed to do. You seemed to
    have answered your own question since you're doing this test in the first
    place.

    > AfxMessageBox ("Second:" +str);
    >
    > }
    >
    > ***
    >
    > Output:
    > First: A
    > Second: // so nothing, why ??
    >
    > Why working only first convetion ? I want convert whole array of uchar,
    > how to do it ?
    >
    Jim Langston, Nov 14, 2006
    #4
  5. Jim Langston wrote:
    > "Cactus" <> wrote in message


    <snip code>

    > CString probably doesn't have a constructor taking an unsigned char * and
    > converting it to a CString (whatever that is)....


    As the op is using AfxMessageBox, this is MFC. It does have such:

    CSTRING_EXPLICIT CStringT( const unsigned char* pszSrc ) :
    CThisSimpleString( StringTraits::GetDefaultManager() )
    {
    *this = reinterpret_cast< const char* >( pszSrc );
    }

    But as it turns out, the op is multi posting this. In an other group it
    was revealed that he is not running the same code he is posting.

    Best, Dan.
    Dan Bloomquist, Nov 14, 2006
    #5
    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. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    569
    Jack Klein
    May 9, 2005
  2. Ioannis Vranos
    Replies:
    11
    Views:
    742
    Ioannis Vranos
    Mar 28, 2008
  3. Ioannis Vranos

    Padding bits and char, unsigned char, signed char

    Ioannis Vranos, Mar 28, 2008, in forum: C Programming
    Replies:
    6
    Views:
    599
    Ben Bacarisse
    Mar 29, 2008
  4. Alex Vinokur
    Replies:
    9
    Views:
    768
    James Kanze
    Oct 13, 2008
  5. pozz
    Replies:
    12
    Views:
    715
    Tim Rentsch
    Mar 20, 2011
Loading...

Share This Page