简体   繁体   中英

Character set with std::string, cout, ofstream and sockets

I'm trying to send strings over a windows socket connection that contain characters like "á" that are outside the ASCII table. I hear garbage comming out the other end when I try something like this:

std::string message("á");
retval = send(conn_socket, message.c_str(), message.length(), 0);

So I started investigating and I came across the need to encode using something like UTF8. While I'm still looking for a simple way to do this in C++, I have a question about the streams behavior I noticed:

How come std::ofstream << message will correctly output á , while cout << message will output garbage? What's the difference between the 2? Can I use this to my advantage when sending these characters over a socket?

When you write to a file with std::ofstream the bytes representing the characters are being written to the file directly, and the application that opens the file is responsible for converting those bytes back to a character. When you write to cout the console will interpret the bytes into a character. The console and an application do not have to agree on the byte encoding; the Windows console in particular will insist on a code page interpretation, even if you would rather wish for UTF-8.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM