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分区需 ...
随机推荐
- mysql中group by存在局限性探讨(待续)
现在有一个需求:在2018年游戏充值记录表(字段主要有: user_name , money , game_id , 表有6000w行)查找出哪些人在某段日期内累计充值金额在100~500元范围内的, ...
- UVa 10765 鸽子和炸弹(割点)
https://vjudge.net/problem/UVA-10765 题意: 给一个n个点的无向图,求每个点删去后形成的连通分量数. 思路: 判断割点,如果是割点的话,在dfs的时候计算出删去它后 ...
- 转 : CSS Modules详解及React中实践
https://zhuanlan.zhihu.com/p/20495964 CSS 是前端领域中进化最慢的一块.由于 ES2015/2016 的快速普及和 Babel/Webpack 等工具的迅猛发展 ...
- 第五章:异步Web服务
到目前为止,我们已经看到了许多使Tornado成为一个Web应用强有力框架的功能.它的简单性.易用性和便捷性使其有足够的理由成为许多Web项目的不错的选择.然而,Tornado受到最多关注的功能是其异 ...
- C4 文件和目录:APUE 笔记
C4: 文件和目录 本章主要讨论stat函数及其返回信息,通过修改stat结构字段,了解文件属性. struct stat结构定义如下: struct stat { __dev_t st_dev; / ...
- HAproxy的安装配置及动静分离
/////////////////////////////目录//////////////////////////////////////////一.安装HAproxy二.编写HAproxy启动脚本三 ...
- css实现标题文字过长截取...
css实现网页中文字过长截取... title class应该这样写: .title{ width:300px; white-space:nowrap; overflow:hidden; text-o ...
- 在win7虚拟机中装sql server---待整理
本科学数据库的时候,为了做作业,需要在自己电脑上装sql server.但是每次都装不上,总是有各种小问题通不过.最后问学长,才采用了在虚拟机里装数据库的方法,在虚拟机中可以不用担心弄乱本机系统. 为 ...
- python脚本10_打印斐波那契数列的第101项
#打印斐波那契数列的第101项 a = 1 b = 1 for count in range(99): a,b = b,a+b else: print(b) 方法2: #打印斐波那契数列的第101项 ...
- 微信小程序------小程序初步学习
1:学习微信小程序,首先的会一点前端的基础会比较容易上手,比如:HTML+CSS,JS,HTML5+CSS3: H5+CSS3中的弹性盒子在微信小程序中经常用到,这是必须掌握的.不会的可以去W3C文档 ...