VC笔记 unicode、ascii、16进制bytedata 互转

1、如何创建unicode 项目

选择“Project->Setting”菜单 
(1)选 Win32 Unicode Debug :
切换到“c/C++ ” Tab页 从下拉列表框中选择 “Preprocessor” ,去掉_MBCS,添加UNICODE,_UNICODE(注意逗号隔开) ,如果是MFC程序,还要在

link中,Category选output,将Entry-Point Symbol设为wWinMainCRTStartup

 

2、unicode码格式存放的16进制数据显示成可见的字符

例子: 如何在电脑上显示出 0x6211 0x4eec ?

代码:
 //我们:  我们  0x6211 0x4eec

 TCHAR MyWchar[128];
 //WCHAR MyWchar[128];
 memset(MyWchar, 0, sizeof(MyWchar));

 MyWchar[0] = 0x6211;
 MyWchar[1] = 0x4eec;
 MyWchar[2] = 25105;
 MyWchar[3] = 20204;

 int a = ((6*16 + 2)*16 + 1)*16 + 1;
 MessageBox(MyWchar);

 

3、Ascii码格式存放的16进制数据显示成可见的字符

 同2,

具体代码:

// ab: 87,98

 char str[20] = {97,98,0};
 MessageBox(str);

 

转换函数:

/**
*把unicode形式的字节数据转成字符格式的字串

例如:"62114eec"-> "我们"
*/
TCHAR* UnicoidByteData2Char(char* pByteIn,int nLenInByte, TCHAR* pCharOut)
{
 if(nLenInByte <= 0 || pByteIn == NULL || pCharOut == NULL)
 {
  MessageBox(_T("UnicoidByteData2Char: 传入解析参数错误!"));
  return NULL;
 }


 char temp[5] = {0};
 int intValue = 0;
 TCHAR* pOut = pCharOut;

 for(int i=0; i<nLenInByte/2; i++)
 {
  memset(temp, 0, sizeof(temp));
  temp[0] = pByteIn[4*i];
  temp[1] = pByteIn[4*i + 1];
  temp[2] = pByteIn[4*i + 2];
  temp[3] = pByteIn[4*i + 3];

  intValue = 0;
  for(int j=0; j<4; j++)
  {
   if(temp[j]>='a' && temp[j] <='f')
   {
    intValue = intValue*16 + (temp[j] - 'a') + 10;
   }
   else if(temp[0]>='A' && temp[j] <='F')
   {
    intValue = intValue*16 + (temp[j] - 'A') + 10;
   }
   else if(temp[0]>='0' && temp[j] <='9')
   {
    intValue = intValue*16 + (temp[j] - '0');
   }
   else
   {
    MessageBox(_T("UnicoidByteData2Char:STK 数据有误"));
    return NULL;
   }
  }

   
  //swprintf(&pOut[i],_T("%d"), intValue);
  pOut[i] = intValue;
 }

 return pOut;
}

 

/**
*把ascii形式的字节数据转成字符格式的字串

例如:"61623031323334" -> "ab01234"
*/
char* AsciiByteData2Char(char* pByteIn,int nLenInByte, char* pCharOut)
{
 if(nLenInByte <= 0 || pByteIn == NULL || pCharOut == NULL)
 {
  MessageBox(_T("AsciiByteData2Char: 传入解析参数错误!"));
  return NULL;
 }


 char temp[3] = {0};
 int intValue = 0;
 char* pOut = pCharOut;

 for(int i=0; i<nLenInByte; i++)
 {
  memset(temp, 0, sizeof(temp));
  temp[0] = pByteIn[2*i];
  temp[1] = pByteIn[2*i + 1];

  //十位数
  if(temp[0]>='a' && temp[0] <='f')
  {
   intValue = 16*(temp[0] - 'a');
  }
  else if(temp[0]>='A' && temp[0] <='F')
  {
   intValue = 16*(temp[0] - 'A');
  }
  else if(temp[0]>='0' && temp[0] <='9')
  {
   intValue = 16*(temp[0] - '0');
  }
  else
  {
   MessageBox(_T("AsciiByteData2Char:STK 数据有误"));
   return NULL;
  }

  //个位数
  if(temp[1]>='a' && temp[1] <='f')
  {
   intValue += (temp[1] - 'a') + 10;
  }
  else if(temp[1]>='A' && temp[1] <='F')
  {
   intValue += (temp[1] - 'A') + 10;
  }
  else if(temp[1]>='0' && temp[1] <='9')
  {
   intValue += (temp[1] - '0');
  }
  else
  {
   MessageBox(_T("STK 数据有误"));
   return NULL;
  }

   
  sprintf(&pOut[i],"%c", intValue);
 }

 

 return pOut;
}

 

 

 /**
*把ascii形式的字串转换成unicode字串
*/
int Ascii2Unicode(char *pchIn, CString *pstrOut)
{
    int nLen;
    WCHAR *ptch;
 
    if(pchIn == NULL)
    {
        return 0;
    }
 
 //WideCharToMultiByte(); //unicode 2 ascii
 //MultiByteToWideChar(); //ascii 2 unicode
    
 nLen = MultiByteToWideChar(CP_ACP, 0, pchIn, -1, NULL, 0);
    ptch = new WCHAR[nLen];
    MultiByteToWideChar(CP_ACP, 0, pchIn, -1, ptch, nLen);
    pstrOut->Format(_T("%s"), ptch);
    
    delete [] ptch;
 
    return nLen;
}

 

 

 输入参数1 @buf ascii码字符串 
输入参数2 @len 字符串buf的长度 
返回对应的hex进制的值 
4、例如: 输入"23abcfe"字符串, 输出的hex值为: 0x23abcfe 
 

UINT ascii2hex(char *buf, int len)  
{  
    int i = 0;
 int j = 0;  
    unsigned int ret = 0;  
    char p[2] = {0};  
    int tmp = 1; 

    for(i = 0; i<len; i++)
 {  
        memcpy(p,&buf[i],1);     
        switch (p[0]){  
  case 'a':                 
   tmp = 1;  
   for(j=0;j<len-1-i;j++)  
    tmp *= 16;  
   ret += 10*tmp;  
   break;  
  case 'b':             
   tmp = 1;  
   for(j=0;j<len-1-i;j++)  
    tmp *= 16;  
   ret += 11*tmp;  
   break;  
  case 'c':                 
   tmp = 1;  
   for(j=0;j<len-1-i;j++)  
    tmp *= 16;  
   ret += 12*tmp;  
   break;  
  case 'd':                 
   tmp = 1;  
   for(j=0;j<len-1-i;j++)  
    tmp *= 16;  
   ret += 13*tmp;  
   break;  
  case 'e':                 
   tmp = 1;  
   for(j=0;j<len-1-i;j++)  
    tmp *= 16;  
   ret += 14*tmp;  
   break;  
  case 'f':                 
   tmp = 1;  
   for(j=0;j<len-1-i;j++)  
    tmp *= 16;  
   ret += 15*tmp;  
   break;  
  default:  
   tmp = 1;  
   for(j=0;j<len-1-i;j++)  
    tmp *= 16;  
   ret += atoi(p)*tmp;  
   break;  
        }  
    } 

    return ret;  
}  

 

/*
*
* 把ascii形式的字串转换成16进制数
* 例如:"ab01234" -> "61623031323334"
*/
char* CStkTreeDlg::AscToHex(char* pInData, char* pOutData)
{
 char* pIn = pInData;
 char* pOut = pOutData;
 
 int hexValue = 0;

 for(int i=0; i<strlen(pIn); i++)
 { 
  hexValue = pIn[i];

  sprintf(pOut + 2*i, "%0x", hexValue);
 }
    return pOut;
}

 

/*
*
* 把CString形式的字串转换成16进制数
5、* 例如:"我们0123"->"62114eec0030003100320033"
*/
char* CStkTreeDlg::CStringToHexByteStr(CString strIn, char* pOutHexStr)
{
 int len = strIn.GetLength();
 if((pOutHexStr == NULL) || (len <= 0))
 {
  return NULL;
 }

 uint16* text = NULL;
 text = (uint16 *)new uint16[len + 1];
 if(text == NULL)
 {
  return NULL;
 }

 char * pOut = pOutHexStr; 
 
 //取得内存内容
 uint16* ptext = strIn.GetBuffer(len);
 wcscpy(text, ptext);

 //转成16进制数
 int index = 0;
 for(int i=0; i<len; i++)
 {
  if(text[i] > 0xff)
  {
   sprintf(pOut+index, "%0x", text[i]);
   index += 4;
  }
  else
  {
   sprintf(pOut+index, "%s", "00");
   index += 2;

   sprintf(pOut+index, "%0x", text[i]);
   index += 2;
  }
  
 }

 delete [] text;

 return pOut;
}

 

 

/*
*
6* 把unicode形式的字串转换成ascii字串
*//Cstring 转 char
*/
char* CStkTreeDlg::CStringToChar(CString szItem, char* pOutChars)      

{
 int len = WideCharToMultiByte(CP_ACP,0,szItem,szItem.GetLength(),NULL,0,NULL,NULL);
 
 char * pOutStr = pOutChars; 
 if(pOutStr == NULL)
 {
  return NULL;
 }
 WideCharToMultiByte(CP_ACP,0,szItem,szItem.GetLength(),pOutStr,len,NULL,NULL);  
 pOutStr[len] = '\0';
 
 return pOutStr;
}

7* 把unicode形式的字串转换成ascii字串

************************************************************************/
/* 输入efbbbf字符串, 输出"efbbbf"       取得utf-8文件的前三个字符,用于比较文件类型
                                                                 */

/************************************************************************/
char* CStkTreeDlg::Adr2Char(char *pIn, char *pOut)
{
int value1 = *(pIn+0) & 0xff;
int value2 = *(pIn+1) & 0xff;
int value3 = *(pIn+2) & 0xff;
char ch1[3] = {0};
char ch2[3] = {0};
char ch3[3] = {0};

sprintf(ch1, "%X", value1);
sprintf(ch2, "%X", value2);
sprintf(ch3, "%X", value3);
sprintf(pOut,"%s%s%s", ch1, ch2, ch3);

return pOut;
}


8* 判断文件类型

/************************************************************************/
/* get file type : EF BB BF   UTF-8 1
FF FE     UNICODE 2
FE FF     UNICODE BIG EDIAN 3
ELSE      ASCII 0
*
/************************************************************************/
int CStkTreeDlg::GetFileType()
{
char fileBegin[4];
memset(fileBegin, 0, sizeof(fileBegin));
m_File.Read(fileBegin, 3);

char strType[7];
memset(strType, 0, sizeof(strType));
Adr2Char(fileBegin, strType);

if((memcmp(strType, "efbbbf", 6) == 0) || (memcmp(strType, "EFBBBF", 6) == 0)){
m_fileType = 1;  //utf-8
}else if((memcmp(strType, "fffe", 4) == 0) || (memcmp(strType, "FFFE", 4) == 0)){
m_fileType = 2;  //UNICODE
}else if((memcmp(strType, "feff", 4) == 0) || (memcmp(strType, "FEFF", 4) == 0)){
m_fileType = 3;  //UNICODE BIG EDIAN
}else{
m_fileType = 0;  //ASCII
}

return m_fileType;
}


 // 根据语言取得配置文件中的字符串

CString LoadMutiLanString(CString szID)
{
 CString szValue;
 DWORD dwSize = 1000;
 TCHAR szStylesPath[_MAX_PATH];
 CString strStylesPath;
 
 VERIFY(::GetModuleFileName(AfxGetApp()->m_hInstance, szStylesPath, _MAX_PATH));  
 strStylesPath = szStylesPath;
 int nIndex  = strStylesPath.ReverseFind(_T('\\'));
 if (nIndex > 0)
 {
  strStylesPath = strStylesPath.Left(nIndex);
 }
 else 
 {
  strStylesPath.Empty();
 }
 strStylesPath += _T("\\Lang\\");

 switch (theApp.m_sSettingOnPc.sSystem.nLang)
 {
 case LANG_TYPE_ENG_USA:
  {
   GetPrivateProfileString(_T("String"),szID,_T("Not found"),
   szValue.GetBuffer(dwSize),dwSize, strStylesPath + _T("English.ini"));
  }
  break; 

 case LANG_TYPE_CH_SIMP:
  {
   GetPrivateProfileString(_T("String"),szID,_T("Not found"),
   szValue.GetBuffer(dwSize),dwSize,strStylesPath + _T("SimpleChinese.ini"));
  }
  break;

 default:
  break;
 }
 
  szValue.ReleaseBuffer();
  szValue.Replace(_T("\\n"),_T("\n")); 

 return szValue;
}





WideCharToMultiByte和MultiByteToWideChar函数的用法 

转自:http://www.cnblogs.com/gakusei/articles/1585211.html

为了支持Unicode编码,需要多字节与宽字节之间的相互转换。这两个系统函数在使用时需要指定代码页,在实际应用过程中遇到乱码问题,然后重新阅读《Windows核心编程》,总结出正确的用法。
WideCharToMultiByte的代码页用来标记与新转换的字符串相关的代码页。
MultiByteToWideChar的代码页用来标记与一个多字节字符串相关的代码页。
常用的代码页由CP_ACP和CP_UTF8两个。
使用CP_ACP代码页就实现了ANSI与Unicode之间的转换。
使用CP_UTF8代码页就实现了UTF-8与Unicode之间的转换。
下面是代码实现:
1.  ANSI to Unicode
wstring ANSIToUnicode( const string& str )
{
 int  len = 0;
 len = str.length();
 int  unicodeLen = ::MultiByteToWideChar( CP_ACP,
            0,
            str.c_str(),
            -1,
            NULL,
            0 );  
 wchar_t *  pUnicode;  
 pUnicode = new  wchar_t[unicodeLen+1];  
 memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));  
 ::MultiByteToWideChar( CP_ACP,
         0,
         str.c_str(),
         -1,
         (LPWSTR)pUnicode,
         unicodeLen );  
 wstring  rt;  
 rt = ( wchar_t* )pUnicode;
 delete  pUnicode; 
 
 return  rt;  
}
2.  Unicode to ANSI
string UnicodeToANSI( const wstring& str )
{
 char*     pElementText;
 int    iTextLen;
 // wide char to multi char
 iTextLen = WideCharToMultiByte( CP_ACP,
         0,
         str.c_str(),
         -1,
         NULL,
         0,
NULL,
         NULL );
 pElementText = new char[iTextLen + 1];
 memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
 ::WideCharToMultiByte( CP_ACP,
         0,
         str.c_str(),
         -1,
         pElementText,
         iTextLen,
         NULL,
         NULL );
 string strText;
 strText = pElementText;
 delete[] pElementText;
 return strText;
}
3.  UTF-8 to Unicode
wstring UTF8ToUnicode( const string& str )
{
 int  len = 0;
 len = str.length();
 int  unicodeLen = ::MultiByteToWideChar( CP_UTF8,
            0,
            str.c_str(),
            -1,
            NULL,
            0 );  
 wchar_t *  pUnicode;  
 pUnicode = new  wchar_t[unicodeLen+1];  
 memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));  
 ::MultiByteToWideChar( CP_UTF8,
         0,
         str.c_str(),
         -1,
         (LPWSTR)pUnicode,
         unicodeLen );  
 wstring  rt;  
 rt = ( wchar_t* )pUnicode;
 delete  pUnicode; 
 
 return  rt;  
}
4.  Unicode to UTF-8    
string UnicodeToUTF8( const wstring& str )
{
 char*     pElementText;
 int    iTextLen;
 // wide char to multi char
 iTextLen = WideCharToMultiByte( CP_UTF8,
         0,
         str.c_str(),
         -1,
         NULL,
         0,
         NULL,
         NULL );
 pElementText = new char[iTextLen + 1];
 memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) );
 ::WideCharToMultiByte( CP_UTF8,
         0,
         str.c_str(),
         -1,
         pElementText,
         iTextLen,
         NULL,
         NULL );
 string strText;
 strText = pElementText;
 delete[] pElementText;
 return strText;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值