Windows 文件编码

//删除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; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值