当前位置: 首页 > news >正文

Linux下wcout输出中文:迄今为止讲得最清楚的

#include <cstdint>
#include <string>
#include <iostream>
#include <locale>
#include <codecvt>
using namespace std;wstring utf8_to_wchar(const string& str);int main() {// locale(const char* name)构造函数根据name创建locale对象// auto lkl = locale(); // 输出乱码// auto lkl = locale(NULL); // 编译不过// 下面取自环境变量LANG,我这里LANG=zh_CN.UTF-8;LC_ALL未定义auto lkl = locale("");locale::global(lkl);string str = "测试"; // 可以是GBK等,不是必须是utf-8wstring_convert<codecvt_utf8<wchar_t>> converter;wstring wide_str = converter.from_bytes(str);wstring ws2 = utf8_to_wchar(str);wcout.imbue(lkl); // imbue: fill sb/sth with strong feelings, opinions or values// Well, fuck the one who chose `imbue' 1000 times!! :-)wcout << wide_str << ws2 << endl;// gcc -finput-charset=charset// Set the input character set, used for translation from the character set of the input file// to the source character set used by GCC.// If the locale does not specify (这是啥子语法), or GCC cannot get this information from the locale,// the default is UTF-8.// specify v [Tn, Tf, Tw]// T: transitive (of a verb) that is used with a direct object either expressed or understood// The regulations specify that you may use a dictionary in the examination. 规则中指明考试时可以用词典。wcout << L"一下" << endl;return 0;
}wstring utf8_to_wchar(const string& str) {wstring result;for (size_t i = 0; i < str.size();) {const uint8_t c = str[i];if (c <= 0x7F) { // 单字节ASCII字符result += wchar_t(c);++i;} else if ((c & 0xE0) == 0xC0) { // 双字节UTF-8字符++i;if (i >= str.size()) break;const uint8_t c1 = str[i];const wchar_t wc = ((c & 0x1F) << 6) | (c1 & 0x3F);result += wc;++i;} else if ((c & 0xF0) == 0xE0) { // 三字节UTF-8字符if (i + 2 >= str.size()) break;const uint8_t c1 = str[i + 1];const uint8_t c2 = str[i + 2];const wchar_t wc = ((c & 0x0F) << 12) | ((c1 & 0x3F) << 6) | (c2 & 0x3F);result += wc;i += 3;} else // 无效字符,跳过++i;}return result;
}
if [ -z "${LANG+x}" ];   then echo "LANG is undefined"; fi
if [ -z "${LC_ALL+x}" ]; then echo "LC_ALL is undefined"; fi

wc, water closet, 厕所,名副其实。

#include <locale.h>

char *setlocale(int category, const char *locale);

The setlocale() function is used to set or query the program's current locale.

If locale is not NULL, the program's current locale is modified according to the arguments.

The argument category determines which parts of the program's current locale should be modified.

  • LC_ALL All of the locale
  • LC_ADDRESS Formatting of addresses and geography-related items (*)
  • LC_COLLATE String collation
  • LC_CTYPE Character classification
  • LC_IDENTIFICATION Metadata describing the locale (*)
  • LC_MEASUREMENT Settings related to measurements (metric versus US customary) (*)
  • LC_MESSAGES Localizable natural-language messages
  • LC_MONETARY Formatting of monetary values
  • LC_NAME Formatting of salutations for persons (*)
  • LC_NUMERIC Formatting of nonmonetary numeric values
  • LC_PAPER Settings related to the standard paper size (*)
  • LC_TELEPHONE Formats to be used with telephone services (*)
  • LC_TIME Formatting of date and time values
http://www.jsqmd.com/news/35852/

相关文章:

  • CCPC哈尔滨站-J. 幻想乡的裁判长
  • C语言中的整型提升
  • 完整教程:Hive 知识点梳理
  • OZI-Project代码注入漏洞分析与修复方案
  • 创建第一个pygame游戏窗口
  • 常量的二元图景:C 语言的刚性契约与 Python 的柔性表达
  • Swift 进行验证码识别:集成 Tesseract OCR
  • 700.二叉搜索树中的搜索(二叉树算法) - 实践
  • egg-passport 的原理, 是否依赖数据库
  • P10194 [USACO24FEB] Milk Exchange G 做题记录
  • egg-sequelize 原理, 访问 sequelize 的方式, 支持情况
  • 创建conda环境时将要安装的一些软件包分析
  • 图书馆管理系统需求规格说明书
  • 含错方程与非线性滤波模型的逼近攻击
  • 重生之我在大学自学鸿蒙构建第一天-《基础篇》
  • 点云配准基础知识
  • 完整教程:Android监听第三方播放获取音乐信息及包名
  • git的各种HEAD以及使用示例
  • OneDrive上传和下载速度慢?有什么解决办法吗? - 指南
  • 详细介绍:深入浅出MATLAB数据可视化:超越plot()
  • 既然道可道相当道,那么传道授业解惑的根基是什么?
  • P10592 BZOJ4361 isn
  • 阿道夫
  • 软件开发公司常犯的5个设计误区,看看你中招了吗?
  • 使用jmeter做压力测试 - 实践
  • CSP2025游记总结
  • 连续出现的字符
  • 详解WebSocket及其妙用 - 指南
  • 2025 csp_j 游忌
  • 利用序列ID漏洞下载整个公司用户数据库的技术分析