CByteArray ba;
....
....CString str;
str.Format("%s" , (LPCTSTR) ba.GetData());
str.SetAt(ba.GetSize() , '\0');
AfxMessageBox(str);

解决方案 »

  1.   

    直接CString str(ba.GetData())不行吗?
      

  2.   

    to Fantasia:
    可以!another way:ba.Add(0); //treat as '\0'
    CString str(ba.GetData());
      

  3.   

    zhq2000,你的这段代码我用了有问题啊
    str.Format("%s" , (LPCTSTR) ba.GetData());
    str.SetAt(ba.GetSize() , '\0');下面是错误对话框:
    标题:Microsoft Visual C++ Debug Library
    内容:program:XXXXXXXXXXXXXXXXXXX
    File:strcore.cpp
    Line:604For information on how your program can cause an assertion failure,see the Visual C++ documentation on asserts.
    下面就是重试,终止,忽略。
    你给我的代码是不是有问题啊?被assert给找出来了?
      

  4.   

    你的第二种方法里面的str是什么?Cstring变量?
      

  5.   

    ba数组的中间是否有值为零的元素? , 如果有,结果肯定不正确,因为零是作为字符串的结束符.#include<afx.h>
    #include<afxcoll.h>
    #include<stdio.h>void main()
    {
    CByteArray ba;
    for(BYTE i=65;i<75;i++)
    ba.Add(i);
              ba.Add(0); // as '\0'

    CString str(ba.GetData());
             // or ..
    //str.Format("%s" , (LPCTSTR)ba.GetData());
    //str.SetAt(ba.GetSize()-1 ,'\0');
    printf("the byte array is : %s\n" , (LPCTSTR)str);
    }