void CUnicodeDlg::OnButtonCode()//编码按键 
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
char user_msg[1024];
memset(user_msg,0,1024);
strcpy(user_msg,m_in);//m_in为对应输入编辑框的CString变量
m_out=strupr(UnicodeToHexString(user_msg));//把数据赋值给edit控件,m_out为对应输出编辑框的CString变量
UpdateData(FALSE);
}void CUnicodeDlg::OnButtonDecode()//解码按键
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
char in_msg[1024];
char out_msg[1024]; memset(in_msg,0,1024);
memset(out_msg,0,1024);

strcpy(in_msg,m_in);
//strcpy(out_msg,HexStringToUnicode(in_msg));
m_out=HexStringToUnicode(strlwr(in_msg)); //m_out=out_msg; UpdateData(FALSE);}char* CUnicodeDlg::UnicodeToHexString(char *pSrc)//编码程序
{
if(pSrc ==NULL)
return "";
int nSrclength = strlen(pSrc);//多字节(短字符串)的长度
int  nDstlength;//宽字符串的长度
char buf0[1];//宽字符高位
char buf1[1];//宽字符低位
wchar_t wchar[128];//每个单元占两个字节
nDstlength = MultiByteToWideChar(CP_ACP,0,pSrc,nSrclength,wchar,128); static char p[256];
memset(p,0,256);
memset(buf0,0,1);
memset(buf1,0,1); for(int i =0;i<nDstlength;i++)
{
sprintf(buf0,"%02x",wchar[i]>>8);//取高字节
sprintf(buf1,"%02x",wchar[i]&0xff);//取低字节
strcat(p,buf0);
strcat(p,buf1);
}
return p;
}char* CUnicodeDlg::HexStringToUnicode(char *pSrc)//解码程序
{
if(pSrc ==NULL)
return NULL;
int nSrcLength = strlen(pSrc);
int  nDstLength;
char temp[256];
static char p[256];
wchar_t wchar[128]; for(int i=0;i<nSrcLength/2;i++)
{
temp[i]=0;
temp[i]=(*(pSrc+i*2)>='A')?(*(pSrc+i*2)-'a'+10):(*(pSrc+i*2)-'0');
temp[i]*=16;
temp[i]+=(*(pSrc+i*2+1)>='A')?(*(pSrc+i*2+1)-'a'+10):(*(pSrc+i*2+1)-'0');
}
temp[i]='\0'; memset(p,0,256);

for(i=0; i<nSrcLength/4; i++)
    {
wchar[i] = temp[i*2] << 8;    // 先高位字节
wchar[i] |= temp[i*2+1];        // 后低位字节
    }
wchar[i]='\0'; nDstLength = WideCharToMultiByte(CP_ACP, 0, wchar, nSrcLength/4, p, 160, NULL, NULL);
p[nDstLength*2]='\0';
return p;
}
以上是我半写半抄袭的程序,编码程序用于GSM模块发送中文短信,解码程序用于接收解码。
输入“你好你好”编码输出“4F60597D4F60597D”。正确
输入“4F60597D4F60597D”解码输出“你好你好”。正确

输入“测试已经就绪”编码输出“6D4B8BD55DF27ECF5C317EEA”。正确
输入“6D4B8BD55DF27ECF5C317EEA”解码输出“测???就?”。错误
出现的问题就是:编码都是正确的(别人的程序也是得出和我相同的编码),但解码时会有一部分字解码成“?”。
这是怎么回事呢?大家帮帮忙,谢谢了。

解决方案 »

  1.   


    void CTest_uniDlg::OnButtonCode()//编码按键 
    {
        // TODO: Add your control notification handler code here
        UpdateData(TRUE);
        char user_msg[1024];
        char xxx[1024];
        memset(user_msg,0,1024);
        strcpy(user_msg,m_in);//m_in为对应输入编辑框的CString变量
        m_out=UnicodeToHexString(user_msg, xxx);//把数据赋值给edit控件,m_out为对应输出编辑框的CString变量
        UpdateData(FALSE);
    }void CTest_uniDlg::OnButtonDecode()//解码按键
    {
        // TODO: Add your control notification handler code here
        UpdateData(TRUE);
        char in_msg[1024];
        char out_msg[1024];    memset(in_msg,0,1024);
        memset(out_msg,0,1024);
        
        strcpy(in_msg,m_in);
        //strcpy(out_msg,HexStringToUnicode(in_msg));
        m_out=HexStringToUnicode(in_msg, out_msg );    //m_out=out_msg;    UpdateData(FALSE);}char* CTest_uniDlg::UnicodeToHexString(char *pSrc, char *out)//编码程序
    {
        if(pSrc ==NULL)
            return "";
        int nSrclength = strlen(pSrc);//多字节(短字符串)的长度
        int  nDstlength;//宽字符串的长度
        wchar_t wchar[128];//每个单元占两个字节
    memset( wchar, 0, sizeof( wchar ) );
        nDstlength = MultiByteToWideChar(CP_ACP,0,pSrc,nSrclength,wchar,128); int i;
    int t;
        for( i =0;i<nDstlength;i++)
        {
    t = wchar[ i ];
    sprintf( out + i * 4, "%.4X", t );
        }
        return out;
    }char* CTest_uniDlg::HexStringToUnicode(char *pSrc, char *out)//解码程序
    {
        if(pSrc ==NULL)
            return NULL;
        int nSrcLength = strlen(pSrc);
        int  nDstLength;
        char temp[256];
        static char p[256];
        wchar_t wchar[128]; int j, w; int i;
        for( i=0;i<nSrcLength ;i += 2)
        {
    j = pSrc[ i ];
    w = pSrc[ i + 1 ];
    if( j >= 'A' && j <= 'F' )
    {
    j -= 'A';
    j += 10;
    }
    if( j >= '0' && j <= '9' )
    {
    j -= '0';
    }
    if( j >= 'a' && j <= 'f' )
    {
    j -= 'a';
    j += 10;
    }
    if( w >= 'A' && j <= 'F' )
    {
    w -= 'A';
    w += 10;
    }
    if( w >= '0' && w <= '9' )
    {
    w -= '0';
    }
    if( w >= 'a' && j <= 'f' )
    {
    w -= 'a';
    w += 10;
    }
    temp[ i / 2 ] = ( ( j << 4 ) + w );
        }
        temp[i]='\0';    memset(p,0,256);
        
        for(i=0; i<nSrcLength/4; i++)
        {
            wchar[i] = temp[i*2] << 8;    // 先高位字节
            wchar[i] |= temp[i*2+1];        // 后低位字节
        }
        wchar[i]='\0';    nDstLength = WideCharToMultiByte(CP_ACP, 0, wchar, nSrcLength/4, p, 160, NULL, NULL);
        p[nDstLength*2]='\0';
        return p;
    }
    如果你要在手机上用,这个是正确的
      

  2.   

    嗯~谢谢~
    但我改成你写的程序,还是
    输入“测试已经就绪”编码输出“6D4B8BD55DF27ECF5C317EEA”。正确
    输入“6D4B8BD55DF27ECF5C317EEA”解码输出“测???就?”。错误
    难道是我VC6.0的问题?你的“6D4B8BD55DF27ECF5C317EEA”解码是“测试已经就绪”吗?
      

  3.   


    char* CTest_uniDlg::HexStringToUnicode(char *pSrc, char *out)//解码程序
    {
        if(pSrc ==NULL)
            return NULL;
        int nSrcLength = strlen(pSrc);
        int  nDstLength;
        unsigned char temp[256];
        static char p[256];
        wchar_t wchar[128]; int j, w; int i;
        for( i=0;i<nSrcLength ;i += 2)
        {
    j = pSrc[ i ];
    w = pSrc[ i + 1 ];
    if( j >= 'A' && j <= 'F' )
    {
    j -= 'A';
    j += 10;
    }
    if( j >= '0' && j <= '9' )
    {
    j -= '0';
    }
    if( j >= 'a' && j <= 'f' )
    {
    j -= 'a';
    j += 10;
    }
    if( w >= 'A' && j <= 'F' )
    {
    w -= 'A';
    w += 10;
    }
    if( w >= '0' && w <= '9' )
    {
    w -= '0';
    }
    if( w >= 'a' && j <= 'f' )
    {
    w -= 'a';
    w += 10;
    }
    temp[ i / 2 ] = ( ( j << 4 ) + w );
        }
        temp[i]='\0';    memset(p,0,256);
        
        for(i=0; i<nSrcLength/4; i++)
        {
            wchar[i] = temp[i*2] << 8;    // 先高位字节
            wchar[i] |= temp[i*2+1];        // 后低位字节
        }
        wchar[i]='\0';    nDstLength = WideCharToMultiByte(CP_ACP, 0, wchar, nSrcLength/4, p, 160, NULL, NULL);
        p[nDstLength*2]='\0';
        return p;
    }行了
      

  4.   


    原来是unsigned的问题,真是高手,谢谢~非常感谢~40分真是亏待你了~谢谢~