Unicode文字列とUnicodeデータの相互変換

1680 ワード

//UNICODE UNICODE 
// UNICODE 
int ConvUniStr2Unicode(LPCSTR szUnicodeString, WCHAR *pWchar, int iBuffSize)
{
  int iRet = 0;

  int iUstrLen = strlen(szUnicodeString);
  ASSERT(pWchar);
  ASSERT(iUstrLen%6 == 0);

  int iOffset = 0;
  while((iOffset < iUstrLen) && (iRet < iBuffSize-1))
  {
    const char *pStr = strstr(szUnicodeString + iOffset, "\\u");
    if(pStr)
    {
      UINT wChar = -1;
      sscanf_s(pStr+2, "%X", &wChar);
      pWchar[iRet++] = wChar;
      pWchar[iRet] = 0;
      iOffset += 6;
    }
    else
    {
      break;
    }
  }
  
  return iRet;
}

//UNICODE UNICODE 
// 
int ConvUnicode2UniStr(WCHAR *pWchar, CHAR *pChar, int iBuffSize)
{
  ASSERT(pChar);
  char subBuff[8];

  int iLength = 0;  
  int iMaxLen = wcslen(pWchar);
  for(int i=0; (i<iMaxLen) && ((iLength+6) < (iBuffSize-1)) ; i++)
  {
    sprintf_s(subBuff, "\\u%04X", pWchar[i]);
    strcpy_s(pChar + iLength, iBuffSize, subBuff);
    iLength += 6;
  }
  return iLength;
}

 
 
// 
  CStringA pAchar1 = ("\\u6D4B\\u8BD5");

  WCHAR wChar[10];
  ConvUniStr2Unicode(pAchar1, wChar, 9);

  CHAR AChar[64];
  ConvUnicode2UniStr(wChar, AChar, 64);
  
  OutputDebugStringW(wChar);
  OutputDebugStringA(AChar);

 
//テスト出力
テストu 6 D 4 Bu 8 BD 5