如何验证std :: string的UTF-8编码

时间:2013-08-06 05:44:19

标签: c++ arm qnx

我们在验证包含有效 UTF_8 字符集的std::string时遇到问题。该 问题是我在程序中有一个std::string,在运行时可能会收到几个无效的 UTF-8 字符。 目前我指的是glib / utf8-validate.c文件以供参考。但是glib使用的数据类型不是std::string,所以我不能使用它。

平台是QNX(Linux主机开发)。

1 个答案:

答案 0 :(得分:2)

首先你应该在你的问题中包含QNX的版本,因为例如6.3.2(Neutrino)的gcc版本是3.7。

第二次使用const char * c_str()const;用于c函数。

#include <stdio.h>
#include <dbus/dbus.h>
#include <string>
#include <iostream>

int main(void)
{    
std::string test = "test";

std::cout << dbus_validate_utf8(test.c_str(), 0) << std::endl;
return 0;
}

但是这可能有助于代替使用dbus_validate_utf8?:

How to detect UTF-8 in plain C?

甚至这个:

http://utfcpp.sourceforge.net/