linux 下 Linux 下char转换为wchar_t 设置本地为utf-8编码 以及wchar 的输入输出
LInux下使用mbstowcs函数可以将char转化为wchar_t
函数含义:convert a multibyte string to a wide char string
说明: The behaviour of mbstowcs depends on the LC_CTYPE category of the current locale
返回值: The mbstowcs() function returns the number of wide characters that make up the converted part of the wide-char-acter string, not including the terminating null wide character. If an invalid multibyte sequence was encountered, (size_t) -1 is returned.
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <locale.h>
#include <iostream>
using namespace std;
// 将char类型转化为wchar // locale: 环境变量的值,mbstowcs依赖此值来判断src的编码方式
11 int ToWchar(char* &src, wchar_t* &dest, const char *locale = "zh_CN.utf8"){
if (src == NULL) {
dest = NULL;
return ;
}
//根据环境变量设置locale
setlocale(LC_CTYPE, locale);
//得到转化为需要的宽字符大小
int w_size = mbstowcs(NULL, src, ) + ;
//w_size=0说明mbstowcs返回值为-1。即在运行过程中遇到了非法字符(很有可能使locale没有设置正确)
if (w_size == ) {
dest = NULL;
return -;
}
wcout << "w_size" << w_size << endl;
dest = new wchar_t[w_size];
if (!dest) return -;
int ret = mbstowcs(dest, src, strlen(src)+);
if (ret <= )return -; return ;
}
int main(){
char* str = "中国123";
wchar_t *w_str ;
ToWchar(str,w_str);
wcout << w_str[] << "--" << w_str[] << "--" << w_str[];
delete(w_str);
return ;
}
#include <stdio.h>
int main(void){
int i_number, result;
float f_number;
char c_number, str[];
wchar_t wc_str, ws_str[];
printf( "\n\nEnter an int, a float, two chars and two strings\n");
result = scanf( "%d %f %c %C %s %S",
&i_number, &f_number, &c_number, &wc_str, str, ws_str );
printf( "\nThe number of fields input is %d\n",
result );
printf( "The contents are: %d %f %c %C %s %S\n",
i_number, f_number, c_number, wc_str, str, ws_str);
wprintf( L"\n\nEnter an int, a float, two chars and two strings\n");
result = wscanf( L"%d %f %hc %lc %S %ls",
&i_number, &f_number, &c_number, &wc_str, str, ws_str );
wprintf( L"\nThe number of fields input is %d\n",
result );
wprintf( L"The contents are: %d %f %C %c %hs %s\n",
i_number, f_number, c_number, wc_str, str, ws_str);
}
linux 下 Linux 下char转换为wchar_t 设置本地为utf-8编码 以及wchar 的输入输出的更多相关文章
- CString 转换为 wchar_t *
1.将CString转换为const char* CString str = _T("231222"); std::string strDp = CStringA(str); / ...
- C++中char*与wchar_t*之间的转换
http://blog.163.com/tianshi_17th/blog/static/4856418920085209414977/ 关于C++中的char*与wchar_t*这两种类型的相互转换 ...
- Linux下的Apache和PHP安全设置,如何开启PHP的安全模式
Linux下的Apache和PHP安全设置 PHP安全模式开启,PHP5.3将不再有安全模式. (1) safe_mode:以安全模式运行php; 在php.ini文件中使用如下 safe_mode ...
- Linux下软件安装方法即路径设置
Linux下软件安装方法即路径设置 http://www.cnblogs.com/edward259/archive/2010/07/02/1770066.html
- LINUX下mysql的大小写是否区分设置 转
一.Linux中MySQL大小写详情:1.数据库名严格区分大小写2.表名严格区分大小写的3.表的别名严格区分大小写4.变量名严格区分大小写5.列名在所有的情况下均忽略大小写6.列的别名在所有的情况下均 ...
- linux下mysql的大小写是否区分设置
转:http://blog.csdn.net/qq_29246225/article/details/52293549 一.Linux中MySQL大小写详情:1.数据库名严格区分大小写2.表名严格区分 ...
- 虚拟机下Linux系统如何设置IP地址
虚拟机下Linux系统设置IP地址三种方法 文章来源:https://jingyan.baidu.com/article/ea24bc399ffeb9da62b3318f.html 工具/原料 V ...
- Linux字符模式下如何设置/删除环境变量
Linux字符模式下设置/删除环境变量方法: bash下 设置:export 变量名=变量值 删除:unset 变量名 csh下 设置:setenv 变量名 变量值 删除:unsetenv 变量名 h ...
- Linux和Uboot下eMMC boot分区读写
关键词:eMMC boot.PARTITION_CONFIG.force_ro等. 1. eMMC的分区 大部分eMMC都有类似如下的分区,其中BOOT.RPMB和UDA一般是默认存在的,gpp分区需 ...
随机推荐
- 分布式文档存储数据库(MongoDB)副本集配置
副本集特征: N 个节点的集群 任何节点可作为主节点 所有写入操作都在主节点上 自动故障转移 自动恢复 相关文章: http://www.cnblogs.com/huangxincheng/archi ...
- ng2 quickstart-primeng
1.导入quickstart-angular项目 2.安装primeng npm install primeng 3.安装@angular/animations npm install @angula ...
- 第九篇:Spark SQL 源码分析之 In-Memory Columnar Storage源码分析之 cache table
/** Spark SQL源码分析系列文章*/ Spark SQL 可以将数据缓存到内存中,我们可以见到的通过调用cache table tableName即可将一张表缓存到内存中,来极大的提高查询效 ...
- C# 人民币转成大写
/// <summary> /// 转换人民币大小金额 /// </summary> /// <param name="num">金额</ ...
- 比较好的Json 格式数据
{ "81040753986": [{ "order_info": { "unique_package_reference": " ...
- LNMP 如何安装mongodb ----lnmp一键安装包之后
mongodb 直接下载官方最新包解压就可以使用了. wget -c http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-2.6.4.tgz ta ...
- python PIL/Pillow图像扩展、复制、粘贴处理
http://blog.csdn.net/yuanyangsdo/article/details/60957685
- 1059: [ZJOI2007]矩阵游戏 二分图匹配
https://www.lydsy.com/JudgeOnline/problem.php?id=1059 裸的二分图匹配,行列匹配即可 /****************************** ...
- Hibernate中"二级缓存"配置
实体类 : package cn.happy.entity; public class Emp { private Integer empNo; private String empName; pub ...
- 016PHP基础知识——流程控制(四)
<?php /** * 流程控制(四) do...while * do{ 代码段 * }while(){ * } * 特点:最少会执行一次代码段 */ /*$i=5; do{ echo $i; ...