BYTE by[100];
int num;
memset(by,3,100);
by[98]=0;
by[99] = 0;//-------------------------(执行该语句后,by变量内存见 图1)
CString str = L"abc";
//num = str.GetLength();
str.Format(L"%s",by);//-------------------------(执行该语句后,str变量内存见 图2)
//num = str.GetLength();
BYTE * pv = (BYTE *)&str;
for(num = 0 ;num < 100; num++)
{
by[num] = *pv; pv++;
}//-------------------------(执行该语句后,by变量内存见 图3)
//memcpy((void *)by,(BYTE *)&str,100);为什么by数据会改变呢?图1 和图3 为什么不一样?
图1:
 1
图2:
 2
图3:
 3

解决方案 »

  1.   

    问题出在这里:
    BYTE * pv = (BYTE *)&str;
    改为
    BYTE * pv = (BYTE *)(LPCTSTR)str;
    就不变了
      

  2.   

    BYTE * pv = (BYTE *)&str;
    没有取到数据指针的地址.
      

  3.   

    cstring 重载了lpctstr可以得到字符串的地址,不要忘 了CSTRING是个类。
      

  4.   

    (LPCTSTR) 在这里起什么作用?能否详解
      

  5.   

            BYTE by[100];
    by[99]=0;
    by[98]=0;
    int num;
    memset(by,3,100);
    CString str = L"abc";
    num = str.GetLength();
    str.Format(L"%s",by);
    num = str.GetLength();
    memcpy((void *)by,(BYTE *)(LPCTSTR)str,num*2);
    我需要根据str的长度复制数据个数。
    但是num第一次获取为3
    第二次获取却为0x4d(77).为什么不是50(十进制)呢?。在Unicode下一个字符占两个字节多以 ,memcpy中我用了num*2
      

  6.   


    语句搞反了,num返回49,不包括最后的0。BYTE by[100];memset(by,3,100);by[99]=0;
    by[98]=0;
    int num;CString str = L"abc";
    num = str.GetLength();
    str.Format(L"%s",by);
    num = str.GetLength();
    memcpy((void *)by,(BYTE *)(LPCTSTR)str,num*2);
      

  7.   

    CString::GetBufferLPTSTR GetBuffer( int nMinBufLength );
      

  8.   


    BYTE by[100];
    by[99]=0;
    by[98]=0;
    int num;
    memset(by,3,100);这样填充后,by[99]=3;
    by[98]=3;转换为字符串,字符串结尾的0就不确定了,字符串长度也不确定了。
      

  9.   

    (LPCTSTR) 在这里起什么作用?能否详解
      

  10.   


     LPCTSTR 操作符在 CString 类中被重载了,该操作符的定义是返回缓冲区的地址.