![](/img/trans.png)
[英]C++: Convert vector<char> to a UTF-8 string so that I can send over a Websocket
[英]Convert a char array of escaped UTF-8 octets to a string in C++
我有一个包含一些UTF-8编码的土耳其字符的char数组 - 以转义的八位字节的形式。 因此,如果我在C ++ 11中运行此代码:
void foo(char* utf8_encoded) {
cout << utf8_encoded << endl;
}
它打印\\xc4\\xb0-\\xc3\\x87-\\xc3\\x9c-\\xc4\\x9e
。 我想将此char[]
转换为std::string
以便它包含UTF-8解码值İ-Ç-Ü-Ğ
。 我已将char[]
转换为wstring
但仍打印为\\xc4\\xb0-\\xc3\\x87-\\xc3\\x9c-\\xc4\\x9e
。 我怎样才能做到这一点?
编辑:我不是构建这个char []的人。 它是私有库调用的回调函数的静态长度参数之一。 所以回调函数如下:
void some_callback_function (INFO *info) {
cout << info->some_char_array << endl;
cout << "*****" << endl;
for(int i=0; i<64; i++) {
cout << "-" << info->some_char_array[i];
}
cout << "*****" << endl;
char bar[65] = "\xc4\xb0-\xc3\x87-\xc3\x9c-\xc4\x9e";
cout << bar << endl;
}
INFO
结构的位置是:
typedef struct {
char some_char_array[65];
} INFO;
所以当调用我的回调函数时,输出如下:
\xc4\xb0-\xc3\x87-\xc3\x9c-\xc4\x9e
*****
-\-x-c-4-\-x-b-0---\-x-c-3-\-x-8-7---\-x-c-3-\-x-9-c---\-x-c-4-\-x-9-e-----------------------------
*****
İ-Ç-Ü-Ğ
所以我目前的问题是,我没有得到info->some_char_array
和bar
char数组之间的区别。 我想要的是编辑info->some_char_array
,使其输出为İ-Ç-Ü-Ğ
。
好吧,这是一个少数,从我正在使用的更大的解析器中撕掉。 但是“有点少数”是Boost.Spirit的本质。 ;-)
解析器不仅会解析十六进制转义,还会解析octals( \\123
)和“standard”转义( \\n
)。 在CC0下提供,所以无论你喜欢什么,你都可以使用它。 ;-)
Boost.Spirit是Boost的“仅标题”部分,因此您无需链接任何库代码。 尽管如此,由Spirit标头完成的相当复杂的“魔法”允许以这种方式用C ++源表达的语法在编译时有点困难。
但它运作良好,效果很好。
#define BOOST_SPIRIT_USE_PHOENIX_V3
#include "boost/spirit/include/qi.hpp"
#include "boost/spirit/include/phoenix.hpp"
#include <string>
#include <cstring>
#include <sstream>
#include <stdexcept>
namespace
{
// Helper function: Turn on_error positional parameters into error message.
template< typename Iterator >
std::string make_error_message( boost::spirit::info const & info, Iterator first, Iterator last )
{
std::ostringstream oss;
oss << "Invalid sequence. Expecting " << info << " here: \"" << std::string( first, last ) << "\"";
return oss.str();
}
}
// Wrap helper function with Boost.Phoenix boilerplate, so the function
// can be called from within a parser's [].
BOOST_PHOENIX_ADAPT_FUNCTION( std::string, make_error_message_, make_error_message, 3 )
// Supports various escape sequences:
// - Character escapes ( \a \b \f \n \r \t \v \" \\ )
// - Octal escapes ( \n \nn \nnn )
// - Hexadecimal escapes ( \xnn ) (*)
//
// (*): In C/C++, a hexadecimal escape runs until the first non-hexdigit
// is encountered, which is not very helpful. This one takes exactly
// two hexdigits.
// Declaring a grammer that works given any kind of iterator,
// and results in a std::string object.
template < typename Iterator >
class EscapedString : public boost::spirit::qi::grammar< Iterator, std::string() >
{
public:
// Constructor
EscapedString() : EscapedString::base_type( escaped_string )
{
// An escaped string is a sequence of
// characters that are not '\', or
// an escape sequence
escaped_string = *( +( boost::spirit::ascii::char_ - '\\' ) | escapes );
// An escape sequence begins with '\', followed by
// an escaped character (e.g. "\n"), or
// an 'x' and 2..2 hexadecimal digits, or
// 1..3 octal digits.
escapes = '\\' > ( escaped_character
| ( "x" > boost::spirit::qi::uint_parser< char, 16, 2, 2 >() )
| boost::spirit::qi::uint_parser< char, 8, 1, 3 >() );
// The list of special "escape" characters
escaped_character.add
( "a", 0x07 ) // alert
( "b", 0x08 ) // backspace
( "f", 0x0c ) // form feed
( "n", 0x0a ) // new line
( "r", 0x0d ) // carriage return
( "t", 0x09 ) // horizontal tab
( "v", 0x0b ) // vertical tab
( "\"", 0x22 ) // literal quotation mark
( "\\", 0x5c ) // literal backslash
;
// Error handling
boost::spirit::qi::on_error< boost::spirit::qi::fail >
(
escapes,
// backslash not followed by a valid sequence
boost::phoenix::throw_(
boost::phoenix::construct< std::runtime_error >( make_error_message_( boost::spirit::_4, boost::spirit::_3, boost::spirit::_2 ) )
)
);
}
private:
// Qi Rule member
boost::spirit::qi::rule< Iterator, std::string() > escaped_string;
// Helpers
boost::spirit::qi::rule< Iterator, std::string() > escapes;
boost::spirit::qi::symbols< char const, char > escaped_character;
};
int main()
{
// Need to escape the backslashes, or "\xc4" would give *one*
// byte of output (0xc4, decimal 196). I understood the input
// to be the FOUR character hex char literal,
// backslash, x, c, 4 in this case,
// which is what this string literal does.
char * some_char_array = "\\xc4\\xb0-\\xc3\\x87-\\xc3\\x9c-\\xc4\\x9e";
std::cout << "Input: '" << some_char_array << "'\n";
// result object
std::string s;
// Create an instance of the grammar with "char *"
// as the iterator type.
EscapedString< char * > es;
// start, end, parsing grammar, result object
boost::spirit::qi::parse( some_char_array,
some_char_array + std::strlen( some_char_array ),
es,
s );
std::cout << "Output: '" << s << "'\n";
return 0;
}
这给出了:
Input: '\xc4\xb0-\xc3\x87-\xc3\x9c-\xc4\x9e'
Output: 'İ-Ç-Ü-Ğ'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.