Windows 文件编码

来源:互联网 发布:云服务器端口怎么开启 编辑:程序博客网 时间:2024/06/17 09:58

//删除UFT-8 BOM


string deleteUtf8Bom(string data)
{
 string Result = "";
 if (data.length > 3)
 {
  if ((data[0] == -17) && (data[1] == -69) && (data[2] == -65))
  {
   Result = data.substr(3, data.length() - 3);
  }
 }
 else Result = data;
 return Result;
}

 

//将宽字符写入到数组中,可用于写文件

void CopyWStringToChar(char* derv, std::wstring source, bool addUnicodBom)
{
 if (addUnicodBom)
 {
  derv[0] = 0xFF;
  derv[1] = 0xFE;

  derv += 2;
 }

 memcpy(derv, (char*)source.c_str(), source.length()*2);
}

 

 

//将字符写入到宽字符中,可用于读文件

std::wstring CopyCharToWString(char* source, int length)
{
 wstring newChar = L"";
 int i = 0;
 bool isBigUnicode = false;

 if (length > 2)
 {
  if (source[0] == -2 && source[1] == -1)
  {
   i = 2;
   isBigUnicode = true;
  }
  else if (source[0] == -1 && source[1] == -2)
  {
   i = 2;
   isBigUnicode = false;
  }
  else
  {
   for (int i = 0; i < length; i++)
   {
    if (source[i] == 0)
    {
     if (i % 2 == 0) isBigUnicode = true;
     break;
    }
   }
  }
 }

 for ( ; i < length; i = i + 2)
 {
  if (isBigUnicode)
  {
   newChar += (wchar_t)(source[i] << 8 & 0xFF00) + (wchar_t)(source[i + 1] & 0x00FF);
  }   
  else
  {   
   newChar += (wchar_t)(source[i + 1] << 8 & 0xFF00) + (wchar_t)(source[i] & 0x00FF);   
  }   
 }
 return newChar; 
}

0 0
原创粉丝点击