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

news/2025/11/9 17:30:57/文章来源:https://www.cnblogs.com/funwithwords/p/19204539
#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.mzph.cn/news/960601.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CCPC哈尔滨站-J. 幻想乡的裁判长

statement 给一个长为 \(n\) 的字符串 \(s\),字符集为 \(\{\text{o, v, w}\}\),请输出最长的回文子串,这个子串中一个 \(\text{w}\) 可以看成两个 \(\text{v}\)。 给个例子:\(\text{wwovvvv}\) 是合法的。 数据范围…

C语言中的整型提升

整型提升 什么是整型提升?为什么要使用整型提升?整型提升是如何进行的? 1.整型提升 在c语言中,一些表达式在求值的过程中,操作数可能需要转换为其他类型,这种转换,我们程序员是看不见的,称其为隐式类型转换,而…

牛客网测试题

题目Java解题 public class test05 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);while(scanner.hasNextLine()){String s = scanner.nextLine();char[] chars = s.toCharArra…

完整教程:Hive 知识点梳理

完整教程:Hive 知识点梳理2025-11-09 17:23 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; …

OZI-Project代码注入漏洞分析与修复方案

本文详细分析OZI-Project/ozi-publish中发现的代码注入漏洞CVE-2025-47271,涵盖漏洞影响范围、修复版本、CVSS评分及缓解措施,帮助开发者理解并防范类似安全风险。OZI-Project/ozi-publish 代码注入漏洞 CVE-2025-4…

创建第一个pygame游戏窗口

创建了一个pygame游戏窗口,并且通过循环不断的监听和响应用户事件 如果用户按下了按键就print一句话;如果是点了退出按钮就关闭窗口 import pygamepygame.init() size = (600, 400) screen = pygame.display.set_mod…

常量的二元图景:C 语言的刚性契约与 Python 的柔性表达

常量的二元图景:C 语言的刚性契约与 Python 的柔性表达 引言:被混淆的 “不变性”—— 从字面量与常量的认知错位说起 在程序设计基础教学里,“常量” 是最容易被 “简化到失真” 的概念。为了让初学者快速上手,很…

用 Swift 解析验证码(结合 Tesseract OCR)

环境准备 1.1 安装 SwiftmacOS 自带 Swift,如需更新,可使用: 更多内容访问ttocr.com或联系1436423940 xcode-select --install 然后检查 Swift 版本: swift --version Linux 用户可以从 Swift 官方网站 下载对应版…

Swift 进行验证码识别:集成 Tesseract OCR

环境准备 1.1 安装 Tesseract OCR在 macOS 上可以使用 Homebrew 进行安装: brew install tesseract 更多内容访问ttocr.com或联系1436423940 安装完成后,检查 Tesseract 是否安装成功: tesseract --version 1.2 创建…

【Linux环境编程】2. Linux核心指令(上)

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

700.二叉搜索树中的搜索(二叉树算法) - 实践

700.二叉搜索树中的搜索(二叉树算法) - 实践pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", &q…

egg-passport 的原理, 是否依赖数据库

egg-passport 的原理, 是否依赖数据库原理 egg-passport 是 Egg.js 框架基于 Passport.js 实现的身份认证插件,其核心原理是:集成 Passport.js 生态基于 Passport.js 的策略(Strategy)机制,支持多种身份认证方式(…

P10194 [USACO24FEB] Milk Exchange G 做题记录

思路(暴力 1) 我们可以先想一个最简单的暴力:遍历每一秒,每一秒的时候遍历每个奶牛来模拟题意。 但是发现这样的暴力没有优化的前景,考虑换一种暴力。 思路(暴力 2) 可以先假设每个奶牛的容量都一样大,那么所有…

egg-sequelize 原理, 访问 sequelize 的方式, 支持情况

egg-sequelize 原理, 访问 sequelize 的方式, 支持情况主要功能 egg-sequelize 是 Egg.js 的 Sequelize 插件,主要作用是:集成 Sequelize ORM 到 Egg.js 框架中提供模型自动加载机制支持多数据源配置简化数据库操作与…

Pandas - No difference between Pandas isna() function and isnull()

Pandas - No difference between Pandas isna() function and isnull()The isna() and isnull() functions in Pandas are essentially identical in functionality. Both are used to detect missing or NA (Not Avai…

2025CCPC哈尔滨站游记

总结:三个人在那边搞笑。。。 一开始我一直在搞F,其他两人: xsh: A我会了。 然后切了。 wyx:M很简单。 然后去打代码。 wyx:好像不简单,没事,L很简单。 然后又去打代码,然后调不出来了。 我此时已经发现了 F 题…

创建conda环境时将要安装的一些软件包分析

创建conda环境时将要安装的一些软件包分析核心包python-3.11.14: Python解释器主程序,3.11 版本比 3.13 版本更稳定,科学计算库支持更好pip-25.2: Python的包管理工具,用于从 PyPI 安装第三方包setuptools-80.9.0: …

图书馆管理系统需求规格说明书

图书馆管理系统需求规划设计书 1.系统概述 1.1 项目背景 本项目旨在开发一个现代化的图书馆管理系统,解决传统图书馆管理中存在的查询繁琐、人工操作效率低、读者服务体验差等问题。 1.2 项目目标 实现图书馆业务的全…

含错方程与非线性滤波模型的逼近攻击

对序列密码线性滤波模型的攻击:https://www.cnblogs.com/luminescence/p/19204373 一、问题描述 1. 非线性滤波模型结构 L 级本原 LFSR ↓ 状态序列 \(S^{(i)} = (s_0^{(i)}, s_1^{(i)}, ..., s_{L-1}^{(i)})\) ↓ 非…

重生之我在大学自学鸿蒙构建第一天-《基础篇》

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …