UNSIGNED CHAR data block and STRING format!!

hi i am working in vc.net 2003

i want to convert u_char array to string format which can be used in a textbox or any other control..

and the u_char block is actually filled in from the system. which will contain non-ascii characters(beyond 0x7f) ,,

actually i want to print u_char array on textbox like

ff f4 01 2f as it is in a byte array..

i am sorry for my poor english .. i am not very good at it.. anyways thanks in advance..




Answer this question

UNSIGNED CHAR data block and STRING format!!

  • Jehan Badshah

    If it's unicode, replace string with wstring and ostringstream with wostringstream, and it'll work with the rest of the code I provided.

  • Ponna

    If your build is unicode, you can directly assign it to the CString object.
    else you may have to use any of the string conversion macros appropriate for you (e.g W2A)

    Please refer to MSDN for the macros

  • vivekvishist

    Note that this forum doesn't cover VC 2003.

    This would be one way to do it:

    size_t len = 5;
    u_char* arr = new u_char[len];
    // ... fill array
    std::ostringstream os;
    os << std::hex;
    for(size_t iter = 0; iter < len; ++iter)
    {
      if(iter > 0)
        os << " ";
      os << static_cast<int>(arr[iter]);
    }
    std::string bytestring = os.str();
    delete [] arr;

    bytestring will now contain something like "ff 00 ff 00";



  • UNSIGNED CHAR data block and STRING format!!