打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
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;
}



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

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;
}

 

 

 

 

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
字符编码之间的相互转换 UTF8与GBK
VC知识库文章 - 常用编码详解
《UTF-8与GB2312之间的互换》的改进
字符集与编码问题小结
编码转换的方法(UNICODE/ASCII/UTF
正确理解UNICODE UTF
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服