例如CString Str="0xD1";
    怎么转换成unsigned char 类型的数据;
在线等待,3Q3Q33Q

解决方案 »

  1.   

    将CString型转换为LPBYTE(即unsigned char*)型的数据类型
    LPBYTE CString_To_LPBYTE(CString str) 

    LPBYTE lpb=new BYTE[str.GetLength()+1];  
    for(int i=0;(i<str.GetLength())&&(*(lpb+i) = str.GetAt(i));i++)
    return lpb;
      

  2.   

    我的意思是把十六进制数据0xD1转化成unsigned char 数据。
      

  3.   

    ??
    自己算最简单。
    或者把0x去掉,然后用sscanf("%X")
      

  4.   

    unsigned char *p;
    CString str;
    int length=str.GetLength();
    for(int i=0;i<length;i++)
    p[i]=str.GetAt(i); 
      

  5.   

    自己写个函数吧,还比较麻烦啦!!得到第三和第四个字符,判断第三和第四个是什么字符
    用switch case,然后再转换呗,
    我只会这种笨办法
      

  6.   

    请继续跟贴。我的问题还是没有解决,BCB中只要用函数Strtoint就可以了
      

  7.   

    int HexToInt(char* p)
    {
     int acc=0;
     if(*p++!='0')
      return -1;
     if(*p != 'x' && *p != 'X')
      return -1;
     p++;
     while(*p)
     {
      int bit4;
      if(*p >= '0' && *p <= '9')
       bit4 = *p - '0';
      else if(*p >= 'a' && *p <= 'f')
       bit4 = *p - 'a' + 10;
      else if(*p >= 'A' && *p <= 'F')
       bit4 = *p - 'A' + 10;
      acc = (acc << 4) | bit4;
      p++;
     }
     return acc;
    }
      

  8.   

    ////16进制字符串转换成数字
    UINT64 GetHexValue(CString str)
    {
    UINT64 m_value;
    char m_valueStr[255];
    strcpy(m_valueStr,str);
       int m_currentLength = strlen(m_valueStr);
      char *p = m_valueStr;
    m_value = 0;
    for (int i = 0; i < m_currentLength; i++)
    {
    m_value *= 16;
    if (*p >= '0' && *p <= '9')
    m_value += (*p - '0');
    else if (*p >= 'a' && *p <= 'f')
    m_value += (10 + *p - 'a');
    else if (*p >= 'A' && *p <= 'F')
    m_value += (10 + *p - 'A');
      p++;
    }
    return m_value ;
    }
      

  9.   

    CString Str="0xD1";
    unsigned char uch;sscanf((LPCTSTR)Str,"0x%X",&uch);