Windows 文件编码
2015-07-08 14:16
281 查看
//删除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;
}
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;
}
相关文章推荐
- 根据字体和字符串来计算大小
- getTranslationX
- String、stringbuffer与stringbuilder的区别
- PostgreSQL 代码生成工具选择
- 黑苹果声卡驱动
- 开始使用Android Sutdio(一)下载安装
- gson 自定义对象转换格式
- C#中的异常及解决办法记录
- Tomcat/JVM常见问题排除及性能优化
- 企业级搜索应用服务器Solr4.10.4部署开发详解(2)- Solr使用-创建集合表、存储、查询
- poj 2352 Stars (树状数组)
- opencv3.0 新算法
- android水波纹RippleEffect源码解析
- 二、MongoDB的简单增删改查
- KVC 字典转模型(属性不全导致崩溃的解决方法)
- CentOS Bash漏洞终极修复方法
- c++链表std::list
- 在线扫描PDF JPG 图片上面文字
- html5画饼形图
- XML文件的解析方式