unsigned char c_Wifi={0x11, 0x22, 0x33, 0x44, 0x55, 0x66};CString str = _T("BC:0F:2B:14:49:D9"); 
TCHAR seps = _T(":"); 
TCHAR* BT_token = _tcstok( (LPTSTR)(LPCTSTR)str, seps ); 
i=0;
while( BT_token != NULL ) 
{
CString BT_S;
BT_S.Format(_T("0x%s"),BT_token);

AfxMessageBox(BT_S); BT_token = _tcstok( NULL, seps ); 
i++; }
主要问题 将CString 值存入 unsigned char 数组中
vcCStringunsigned char

解决方案 »

  1.   

    CString本身就是数组,简单的话你可以获取CString长度,for循环写入
      

  2.   

    使用API函数进行转换。// Unicod To ANSI转换
    int CStringProc::UnicodToAnsi(CString str,char* buff)
    {
    int len=0;
    // UNICODE编码 转换成 ANSI编码
    len = ::WideCharToMultiByte(CP_ACP, NULL, str, 
    str.GetLength(), NULL, 0, NULL, NULL); // 取字串长度
    ::WideCharToMultiByte(CP_ACP, NULL, str, str.GetLength(), 
    buff, len, NULL, NULL);
    buff[len++]=0x00; // 写字串尾部结束标志 return len; // 转换长度
    }
      

  3.   

    朋友编码这块熟悉吗?帮我看看这个吧:http://bbs.csdn.net/topics/390460540?page=1#post-394505566
      

  4.   

    稍微修改了下代码
    unsigned char c_Wifi[6]={0x11, 0x22, 0x33, 0x44, 0x55, 0x66}; //注意用数组 CString str = _T("BC:0F:2B:14:49:D9");  
    TCHAR seps = _T(':'); 
    TCHAR* BT_token = _tcstok( (LPTSTR)(LPCTSTR)str, &seps ); 
    int i=0; //这个值没用
    CString cstrShow;
    while( BT_token != NULL ) 
    {
    CString BT_S;
    BT_S.Format(_T("0x%s "),BT_token);
    cstrShow += BT_S;
    BT_token = _tcstok( NULL, &seps ); 
    i++; } AfxMessageBox(cstrShow);
      

  5.   

    看错了。原来楼主要这个//字符串 转 十六进制字节数组,返回数组长度
    int StrToByte(CString str, CByteArray &data)
    {
    BYTE byte[100];
    int nlen = 0;
    int str_length = str.GetLength();
    CString strTemp;
    // 字符串转换为 BYTE 类型
    // sscanf(strTemp, "%02x", &int_temp);
    for (int i = 0; i < str_length; )
    {
    strTemp = str.Mid(i, 2);
    int int_temp = 0;
    sscanf(strTemp, "%02x", &int_temp);
    byte[nlen] = (BYTE)int_temp;
    //
    data.Add(byte[nlen]);
    i += 3;
    nlen++;
    strTemp.ReleaseBuffer();                
    }
    data.SetSize(nlen);return nlen;}