UTF-8 CPP是一个简单、小巧、轻量级、跨平台的UTF-8编码字符串库。

下面对其使用方法进行简单的介绍:

1.      从http://sourceforge.net/projects/utfcpp/下载最新的utf8_v2_3_4.zip源码,将其解压缩;

2.      新建一个vs2013 控制台工程TestUTF-8CPP,将utf-8cpp中的src文件加入到包含目录中;

3.      参考http://utfcpp.sourceforge.net/,测试代码内容为:

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <assert.h>
#include "utf8.h"// checks whether the content of a file is valid UTF-8 encoded text without reading the content into the memory
bool valid_utf8_file(const char* file_name)
{std::ifstream ifs(file_name);if (!ifs)return false; // even better, throw herestd::istreambuf_iterator<char> it(ifs.rdbuf());std::istreambuf_iterator<char> eos;return utf8::is_valid(it, eos);
}// The function will replace any invalid UTF-8 sequence with a Unicode replacement character
void fix_utf8_string(std::string& str)
{std::string temp;utf8::replace_invalid(str.begin(), str.end(), back_inserter(temp));str = temp;
}int main(int argc, char* argv[])
{const char* test_file_path = "../../../demo/test.txt";// Open the test file(contains UTF-8 encoded text)std::ifstream fs8(test_file_path);if (!fs8.is_open()) {std::cout << "Could not open " << test_file_path << std::endl;return -1;}if (!valid_utf8_file(test_file_path))return -1;unsigned line_count = 1;std::string line;// Play with all the lines in the filewhile (getline(fs8, line)) {// check for invalid utf-8 (for a simple yes/no check, there is also utf8::is_valid function)std::string::iterator end_it = utf8::find_invalid(line.begin(), line.end());if (end_it != line.end()) {std::cout << "Invalid UTF-8 encoding detected at line " << line_count << "\n";std::cout << "This part is fine: " << std::string(line.begin(), end_it) << "\n";}// Get the line length (at least for the valid part)int length = utf8::distance(line.begin(), end_it);std::cout << "Length of line " << line_count << " is " << length << "\n";// Convert it to utf-16std::vector<unsigned short> utf16line;utf8::utf8to16(line.begin(), end_it, back_inserter(utf16line));// And back to utf-8std::string utf8line;utf8::utf16to8(utf16line.begin(), utf16line.end(), back_inserter(utf8line));// Confirm that the conversion went OK:if (utf8line != std::string(line.begin(), end_it))std::cout << "Error in UTF-16 conversion at line: " << line_count << "\n";line_count++;}std::string str = "ABCD";std::vector<unsigned short> utf16result;utf8::utf8to16(str.begin(), str.end(), std::back_inserter(utf16result));size_t size1 = utf16result.size();std::string str2 = "濦粿夿旴";std::string utf8str;utf8::utf16to8(str2.begin(), str2.end(), std::back_inserter(utf8str));size_t size2 = utf8str.length();char utf8_with_surrogates[] = "\xe6\x97\xa5\xd1\x88\xf0\x9d\x84\x9e";std::vector <unsigned short> utf16result1;utf8::utf8to16(utf8_with_surrogates, utf8_with_surrogates + 9, back_inserter(utf16result1));assert(utf16result1.size() == 4);assert(utf16result1[2] == 0xd834);assert(utf16result1[3] == 0xdd1e);unsigned short utf16string[] = { 0x41, 0x0448, 0x65e5, 0xd834, 0xdd1e };std::vector<unsigned char> utf8result;utf8::utf16to8(utf16string, utf16string + 5, back_inserter(utf8result));assert(utf8result.size() == 10);char* szSex = "\xe7\x94\xb7\x00";std::basic_string<wchar_t> sex;utf8::utf8to16(szSex, szSex + strlen(szSex), back_inserter(sex));if (sex != L"男") {std::cout << "unicode char utf16 error" << std::endl;return -1;}std::cout << "ok!" << std::endl;return 0;
}

GitHub:https://github.com/fengbingchun/UTF-8CPP_Test

UTF-8 CPP的使用相关推荐

  1. Zip.cpp的例子

    . 简介 我已经介绍了在{A} XZip.本文介绍XZip也XUnzip,这允许您添加ZIP解压缩到您的应用程序,而不使用任何的.lib或.dll. 首先,让我承认的工作和生产的.cpp和.h文件是基 ...

  2. C++ .h(头文件) 与 .cpp(源文件) 的使用

    .h 文件: .h是头文件 ,里面主要是写类的声明(包括类里面的成员和方法的声明).函数原型.#define常数等, 注意.h文件写的时候有特定的格式就是开头和结尾 #ifndef TEST_HEAD ...

  3. 利用VS+MFC+Opencv显示图像和视频所需添加类(CvvImage.h和CvvImage.cpp的源码)。

    CvvImage.h代码: #pragma once #ifndef CVVIMAGE_CLASS_DEF #define CVVIMAGE_CLASS_DEF #include "open ...

  4. 拇指接龙游戏升级记录03(升级MainScene.cpp)

    MainScene是拇指接龙游戏的主游戏场景文件,拥有近5000行代码. 说实在的,实现自cocos2d-x 2.x版本向当下最新的3.8.1版本的升级过程,其中涉及的技术不是一下能够说明的.有些是形 ...

  5. GATB的使用小例子test.cpp

    1.touch test.cpp,,文件夹中 出现test.cpp touch test.cpp 2. test.cpp的内容 #include <gatb/gatb_core.hpp>i ...

  6. Linux下运行.cpp文件

    如何在Ubuntu16下运行一个简单的.cpp文件呢,做法如下: 假设我在桌面上写了一个hell,world程序; 保存为abc.cpp 然后在终端打开: 1.点击保存的文件的属性,看在哪里,我的是在 ...

  7. 2020-10-26runtime error: member access within null pointer of type ‘struct ListNode‘ (solution.cpp)错

    runtime error: member access within null pointer of type 'struct ListNode' (solution.cpp)错误 /*** Def ...

  8. matlab怎么跑.cpp程序,MATLAB编译cpp文件

    目的:打通MATLAB与VC之间的通道,实现用MATLAB调用VC程序,以及VC调用MATLAB程序. 上篇博客实现了将MATLAB的M文件编译成C/C++文件,exe应用程序.这篇实现MATLAB编 ...

  9. python如何调用cpp文件的接口函数_C++中嵌入Python调用

    python嵌入到C++中 把python嵌入的C++里面需要做一些步骤 安装python程序,这样才能使用python的头文件和库 在我们写的源文件中增加"Python.h"头文 ...

最新文章

  1. 洛古 P1373 小a和uim之大逃离
  2. TCP/IP详解 笔记八
  3. 一切为了孩子——一位IT麻麻的新西兰移民记录
  4. 94年出生,她们如今都是985高校博士生导师!
  5. qtreewidget 获取根节点_详解去中心化信任根dRoT技术
  6. 关于myBatis的问题There is no getter for property named 'USER_NAME' in 'class com.bky.model.实例类'
  7. Python scipy拟合分布
  8. “摔杯一怒为俞渝” 当当创始人李国庆:蓄谋已久的阴谋 不吐不快
  9. matlab存储为二进制txt,matlab读取内容为二进制的TXT文件
  10. css 实现一个尖角_(转载) css实现小三角(尖角)
  11. (伪)原创,采集工具应用
  12. 路由配置与管理——ISIS路由配置与管理
  13. mysql只能存1000条数据_为什么我mysql的表添加了1000条记录之后就存不进去数据了,客户端也没报错...
  14. 为云服务器(VPS)增加一个站点支持
  15. 2021年兴义五中高考成绩查询,贵州省黔西南州兴义市第五中学2021年招生计划
  16. ai决策_人工智能时代的决策
  17. 和小公主一起学习Branch and Bound
  18. TEB局部轨迹规划代码解读
  19. 晒晒这些让人崩溃的话
  20. 用c语言计算正四棱锥的体积,《计算机图形学》习题与解答.doc

热门文章

  1. C语言:随笔10--共用体
  2. Python Qt GUI设计:窗口布局管理方法【强化】(基础篇—6)
  3. 力扣(LeetCode)刷题,简单题(第9期)
  4. 深度学习多框架多平台推理引擎工具
  5. 2019南昌网络赛 C题,Hello 2019
  6. WatchClear 方法 html5,clearWatch()
  7. LeetCode刷题记录10——434. Number of Segments in a String(easy)
  8. mysql数据定义语句有哪些_MySQL语法一:数据定义语句 钓鱼翁
  9. AS3.0编程 So本地数据存储(“超级cookies”)--AS3:Local SharedObject
  10. Unity 2021人工智能导论 Introduction to Artificial Intelligence in Unity 2021