// 8-bit解码
  nDstLength = gsmString2Bytes(pSrc, buf, tmp * 2);   // 格式转换我发觉此处格式转换有问题,导致8BIT信息解码都为空,这是为什么?
// 可打印字符串转换为字节数据
// 如:"C8329BFD0E01" --> {0xC8, 0x32, 0x9B, 0xFD, 0x0E, 0x01}
// pSrc: 源字符串指针
// pDst: 目标数据指针
// nSrcLength: 源字符串长度
// 返回: 目标数据长度
int CSms::gsmString2Bytes(const char* pSrc, unsigned char* pDst, int nSrcLength)
{
for(int i=0; i<nSrcLength; i+=2)
{
  // 输出高4位
  if(*pSrc>='0' && *pSrc<='9')
  {
   *pDst = (*pSrc - '0') << 4;
  }
  else
  {
   *pDst = (*pSrc - 'A' + 10) << 4;
  }  pSrc++;  // 输出低4位
  if(*pSrc>='0' && *pSrc<='9')
  {
   *pDst |= *pSrc - '0';
  }
  else
  {
   *pDst |= *pSrc - 'A' + 10;
  }  pSrc++;
  pDst++;
}// 返回目标数据长度
return nSrcLength / 2;
}

解决方案 »

  1.   

    int gsmString2Bytes(const char* pSrc, unsigned char* pDst, int nSrcLength)
    {
    char temp[3];
    for(int i=0; i<nSrcLength / 2; i++)
    {
    temp[0] = pSrc[i*2];
    temp[1] = pSrc[i*2+1];
    pDst[i] = (char)strtoul(temp, NULL, 16);
    } return nSrcLength / 2;
    }
      

  2.   

    #include <iostream.h>
    #include <afx.h>unsigned char * gsmString2Bytes(const char* pSrc,int nSrcLength)
    {
    int temp1=0,temp2=0;
    unsigned char* pDst=new unsigned char[nSrcLength+1];
    memset(pDst,0,nSrcLength+1);
    for (int i=0;i<nSrcLength;i++)
    {
    if (pSrc[i*2]>='0' && pSrc[i*2]<='9') temp1=(int)pSrc[i*2]-48;
    if (pSrc[i*2]>='a' && pSrc[i*2]<='f') temp1=(int)pSrc[i*2]-87;
    if (pSrc[i*2]>='A' && pSrc[i*2]<='F') temp1=(int)pSrc[i*2]-55;
    if (pSrc[i*2+1]>='0' && pSrc[i*2+1]<='9') temp2=(int)pSrc[i*2+1]-48;
    if (pSrc[i*2+1]>='a' && pSrc[i*2+1]<='f') temp2=(int)pSrc[i*2+1]-87;
    if (pSrc[i*2+1]>='A' && pSrc[i*2+1]<='F') temp2=(int)pSrc[i*2+1]-55;
    pDst[i]=temp1*16+temp2;
    }
    return pDst;
    }
    void main()
    {
    char* a="D2F4C0D6BAD0BAC3D0C4B7D6CAD6";
    CString str=a;
    int nSrcLength=str.GetLength()/2;
    cout<<gsmString2Bytes(a,nSrcLength)<<endl;
    }
      

  3.   

    1  根据你的意思我对题中作了一点改动:上面的代码实际上我把你的函数原型改成了unsigned char * gsmString2Bytes(const char* pSrc,int nSrcLength);省去了一个参数,且返回数值的类型也改成了unsigned char *类型了,因为我觉得这样更好一些,同样可以完成你想要的功能;
    2  "D2F4C0D6BAD0BAC3D0C4B7D6CAD6"表示“音乐盒好心分手”这七个汉字的对应编码;
    3  这段代码在我的机器上(XP系统,VC++ 6)上运行通过;
    4  代码中有内存泄露,我没时间了,自己再改善一下。
      

  4.   

    至于你问的“我发觉此处格式转换有问题,导致8BIT信息解码都为空,这是为什么?”那是因为你参数传送的方法不对,不能用传值方法的啦。