本文整理汇总了C++中Encoder::fromBytes方法的典型用法代码示例。如果您正苦于以下问题:C++ Encoder::fromBytes方法的具体用法?C++ Encoder::fromBytes怎么用?C++ Encoder::fromBytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Encoder
的用法示例。
在下文中一共展示了Encoder::fromBytes方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
/**
* This code assumes that the server replies the exact text the client sent it (as opposed to the practical session example)
*/
int main (int argc, char *argv[]) {
if (argc < 3) {
std::cerr << "Usage: " << argv[0] << " host port" << std::endl << std::endl;
return -1;
}
std::string host = argv[1];
short port = atoi(argv[2]);
ConnectionHandler connectionHandler(host, port);
if (!connectionHandler.connect()) {
std::cerr << "Cannot connect to " << host << ":" << port << std::endl;
return 1;
}
// We will send now A Greek string encoded in UTF-8 to the server and get it back. We will play with this string a little:
// A Greek string encoded in UTF-8
// 20 = space = 0020
// ce ba = kappa = 03ba
// e1 bd b9 = omicron with oxia = 1f79
// cf 83 = sigma = 03c3
// ce bc = mu = 03bc
// ce b5 = epsilon = 03b5
// a = new line = 000a
unsigned char greek[] = {0x20, 0xce, 0xba, 0xe1, 0xbd, 0xb9, 0xcf,
0x83, 0xce, 0xbc, 0xce, 0xb5, 0x0a, 0x00};
Encoder encoder;
std::string utf8g (encoder.fromBytes((const char*)greek));
// This will not print nicely in your console because consoles do not like UTF-8
// But if you redirect to a file and open it in a text editor in UTF-8 - it will show nicely
// for example, in Notepad it shows nicely in Greek.
std::cout << std::endl << "Greek string " << utf8g;
// Get the string length in number of character (counts the UTF-8 characters in the string and not the bytes)
int lg = (int)utf8::distance(utf8g.begin(), utf8g.end());
std::cout << "Length of greek is " << lg << " UTF-8 chars in " << strlen(utf8g.c_str()) << " bytes" << std::endl;
// This conversion will fail because there are no ASCII characters that
// correspond to Greek ones. It will print '?' for each unknown character.
try{
std::cout << "confused greek string " << encoder.fromUtf8ToAscii(utf8g) << std::endl << std::endl;
}catch(boost::locale::conv::conversion_error & error){
std::cerr << "conversion failed" << std::endl << std::endl;
}
//send the string to the server:
if (!connectionHandler.sendBytes(encoder.toBytes(utf8g),strlen(utf8g.c_str()))) {
std::cout << "Disconnected. Exiting...\n" << std::endl;
return 1;
}
//get the echo back from the server as simple bytes:
char greekEcho[256];
if (!connectionHandler.getBytes(greekEcho, strlen(utf8g.c_str()))) {
std::cout << "Disconnected. Exiting...\n" << std::endl;
return 1;
}
// A C string must end with a 0 char delimiter. When we filled the answer buffer from the socket
// we filled up to the \n char - we must make sure now that a 0 char is also present. So we replace \n with 0.
// strlen(answer) counts the chars until the 0 char is found.
greekEcho[strlen(utf8g.c_str())]='\0';
// This will not print nicely in your console because consoles do not like UTF-8
// But if you redirect to a file and open it in a text editor in UTF-8 - it will show nicely
// for example, in Notepad it shows nicely in Greek.
std::cout << "Greek echo string " << greekEcho;
std::string utf8gEcho((const char*)greekEcho);
// Get the string length in number of character (counts the UTF-8 characters in the string and not the bytes)
lg = (int)utf8::distance(utf8gEcho.begin(), utf8gEcho.end());
std::cout << "Length of greek echo is " << lg << " UTF-8 chars in " << strlen(utf8gEcho.c_str()) << " bytes" << std::endl << std::endl;
//write the utf8 string to a file so that we can check it in a text editor
encoder.writeUtf8File("output-utf8.txt", utf8gEcho);
// Convert utf8 string to a utf-16 string and write it to a file so that we can check it in a text editor
try{
utf16string utf16gEcho(encoder.fromUtf8ToUtf16(utf8gEcho));
encoder.writeUtf16File("output-utf16.txt", utf16gEcho);
}catch(boost::locale::conv::conversion_error & error){
std::cerr << "conversion failed" << std::endl;
}
//now you can open these files and see that you can read the Greek letters, and that both files are the same (except for the encoding of course).
std::cout << "This is the end of the utf8 test. From now on - Standard echo client\n\n";
//From here we will see the rest of the ehco client implementation:
while (1) {
const short bufsize = 1024;
char buf[bufsize];
std::cin.getline(buf, bufsize);
std::string line(buf);
int len=line.length();
//.........这里部分代码省略.........