西安美国应用材料公司:从宽字符转换到UTF8的代码
来源:百度文库 编辑:九乡新闻网 时间:2024/04/29 04:35:42
http://www.oschina.net/code/snippet_54334_887 Jack.arain 发布于 2010年10月26日 [代码] cpp代码
[代码] cpp代码
(#)/*Copyright (C) 2004-2005 Cory NelsonThis software is provided 'as-is', without any express or impliedwarranty. In no event will the authors be held liable for any damagesarising from the use of this software.Permission is granted to anyone to use this software for any purpose,including commercial applications, and to alter it and redistribute itfreely, subject to the following restrictions:1. The origin of this software must not be misrepresented; you must notclaim that you wrote the original software. If you use this softwarein a product, an acknowledgment in the product documentation would beappreciated but is not required.2. Altered source versions must be plainly marked as such, and must not bemisrepresented as being the original software.3. This notice may not be removed or altered from any source distribution.*/// namespaces added by Arvid Norberg#ifndef __UTF8_H__#define __UTF8_H__#include#include #include #include namespace detail {template wchar_t decode_utf8_mb(InputIterator &iter, InputIterator last){if (iter == last) throw std::runtime_error("incomplete UTF-8 sequence");if (((*iter) & 0xc0) != 0x80) throw std::runtime_error("invalid UTF-8 sequence");return (wchar_t)((*iter++) & 0x3f);}template wchar_t decode_utf8(InputIterator &iter, InputIterator last){wchar_t ret;if (((*iter) & 0x80) == 0) // one byte{ret = *iter++;}else if (((*iter) & 0xe0) == 0xc0) // two bytes{wchar_t byte1 = (*iter++) & 0x1f;wchar_t byte2 = decode_utf8_mb(iter, last);ret = (byte1 << 6) | byte2;}else if (((*iter) & 0xf0) == 0xe0) // three bytes{wchar_t byte1 = (*iter++) & 0x0f;wchar_t byte2 = decode_utf8_mb(iter, last);wchar_t byte3 = decode_utf8_mb(iter, last);ret = (byte1 << 12) | (byte2 << 6) | byte3;}// TODO: support surrogate pairselse throw std::runtime_error("UTF-8 not convertable to UTF-16");return ret;}template OutputIterator utf8_wchar(InputIterator first, InputIterator last, OutputIterator dest){for(; first!=last; ++dest)*dest = decode_utf8(first, last);return dest;}template void encode_wchar(InputIterator iter, OutputIterator &dest){if(*iter <= 0x007F){*dest=(char)*iter;++dest;}else if(*iter <= 0x07FF){*dest = (char)(0xC0 |((*iter & 0x07C0) >> 6));++dest;*dest = (char)(0x80 |(*iter & 0x003F));++dest;}else if(*iter <= 0xFFFF){*dest = (char)(0xE0 |((*iter & 0xF000) >> 12));++dest;*dest = (char)(0x80 |((*iter & 0x0FC0) >> 6));++dest;*dest = (char)(0x80 |(*iter & 0x003F));++dest;}}template OutputIterator wchar_utf8(InputIterator first, InputIterator last, OutputIterator dest){for(; first!=last; ++first)encode_wchar(first, dest);return dest;}}inline void utf8_wchar(const std::string &utf8, std::wstring &wide){wide.clear();detail::utf8_wchar(utf8.begin(), utf8.end(), std::back_inserter(wide));}inline std::wstring utf8_wchar(const std::string &str){std::wstring ret;utf8_wchar(str, ret);return ret;}inline void wchar_utf8(const std::wstring &wide, std::string &utf8){utf8.clear();detail::wchar_utf8(wide.begin(), wide.end(), std::back_inserter(utf8));}inline std::string wchar_utf8(const std::wstring &str){std::string ret;wchar_utf8(str, ret);return ret;}#endif // __UTF8_H__
从宽字符转换到UTF8的代码
宽字符转换的相关代码
Linux下转换字符集(UTF8转换)
特殊字符前面加斜线的代码
jni中字符转换中文乱码的处理方法asf
C/C++ 字符编码的转换(ut8、gb2312)
Delphi字符串、PChar与字符数组之间的转换
jni中字符转换中文乱码的处理方法
附加聊天字符控制代码效果的拼图 (网摘)
一个“从宽”典型的悲剧
李汝高:一个“从宽”典型的悲剧
oracle汉字到拼音的转换
C#实现汉字转换为拼音缩写的代码
(转)ASCII 到UNICODE再到UTF8 - My Way In Programing - ItEye技术网站
Ansi,UTF8,Unicode,ASCII编码的区别 - xiongxiao的专栏 -...
ASCII码表——在线ASCII字符转换
UTF-8 GBK UTF8 GB2312之间的区别和关系
简单的字符设备
VC6工程转换到VC8,FerryMan Fractal遇到的麻烦
如何转换只读的excel2007到excel2003中进行更改
从销售员到总监的成功代码
选中任何字符都会出现在文本框中的代码
很简单实用的繁体与简体互相转换的代码
ASP使用正则表达式批量将HTML代码里的标签大写转换成小写