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 的输入输出的更多相关文章

  1. CString 转换为 wchar_t *

    1.将CString转换为const char* CString str = _T("231222"); std::string strDp = CStringA(str);  / ...

  2. C++中char*与wchar_t*之间的转换

    http://blog.163.com/tianshi_17th/blog/static/4856418920085209414977/ 关于C++中的char*与wchar_t*这两种类型的相互转换 ...

  3. Linux下的Apache和PHP安全设置,如何开启PHP的安全模式

    Linux下的Apache和PHP安全设置 PHP安全模式开启,PHP5.3将不再有安全模式. (1) safe_mode:以安全模式运行php; 在php.ini文件中使用如下 safe_mode ...

  4. Linux下软件安装方法即路径设置

    Linux下软件安装方法即路径设置 http://www.cnblogs.com/edward259/archive/2010/07/02/1770066.html

  5. LINUX下mysql的大小写是否区分设置 转

    一.Linux中MySQL大小写详情:1.数据库名严格区分大小写2.表名严格区分大小写的3.表的别名严格区分大小写4.变量名严格区分大小写5.列名在所有的情况下均忽略大小写6.列的别名在所有的情况下均 ...

  6. linux下mysql的大小写是否区分设置

    转:http://blog.csdn.net/qq_29246225/article/details/52293549 一.Linux中MySQL大小写详情:1.数据库名严格区分大小写2.表名严格区分 ...

  7. 虚拟机下Linux系统如何设置IP地址

    虚拟机下Linux系统设置IP地址三种方法 文章来源:https://jingyan.baidu.com/article/ea24bc399ffeb9da62b3318f.html 工具/原料   V ...

  8. Linux字符模式下如何设置/删除环境变量

    Linux字符模式下设置/删除环境变量方法: bash下 设置:export 变量名=变量值 删除:unset 变量名 csh下 设置:setenv 变量名 变量值 删除:unsetenv 变量名 h ...

  9. Linux和Uboot下eMMC boot分区读写

    关键词:eMMC boot.PARTITION_CONFIG.force_ro等. 1. eMMC的分区 大部分eMMC都有类似如下的分区,其中BOOT.RPMB和UDA一般是默认存在的,gpp分区需 ...

随机推荐

  1. elasticsearch系列(三)库表理解

    首先ES没有库和表的概念,只有index,type,document(详细术语可以看ES的系列一 http://www.cnblogs.com/ulysses-you/p/6736926.html), ...

  2. spark SQL学习(spark连接 mysql)

    spark连接mysql(打jar包方式) package wujiadong_sparkSQL import java.util.Properties import org.apache.spark ...

  3. JavaScript的动态特性(通过eval,call,apply和bind来体现)

    JavaScript的动态特性(通过eval,call,apply和bind来体现) JavaScript是一种基于面向对象的.函数式的.动态的编程语言.现在发展到已经可以用在浏览器和服务器端了. 这 ...

  4. 03_MySQL DQL_排序查询

    #进阶3:排序查询/*语法: select 查询列表 from 表名 [where 筛选条件] order by 排序列表 [asc|desc] 特点: 1.asc升序,desc降序, 如果都不写,默 ...

  5. R语言系列:自定义function

    在用R语言做各种事物时,用户自定义函数是不可或缺的.这期来讲讲如何自定义R的function.首先要介绍的是function的基本框架: myfunction <- function(arg1, ...

  6. shell 条件语句

    shell 条件语句 #!/bin/bash # 条件语句 NUM1=100 NUM2=200 if (($NUM1 > $NUM2));then echo "$NUM1 greate ...

  7. Class 的基本语法

    简介 JavaScript 语言中,生成实例对象的传统方法是通过构造函数.下面是一个例子. function Point(x, y) { this.x = x; this.y = y; } Point ...

  8. 【Python】常用排序算法的python实现和性能分析

    作者:waterxi 原文链接 背景 一年一度的换工作高峰又到了,HR大概每天都塞几份简历过来,基本上一天安排两个面试的话,当天就只能加班干活了.趁着面试别人的机会,自己也把一些基础算法和一些面试题整 ...

  9. [Spring]Spring Mvc实现国际化/多语言

    1.添加多语言文件*.properties F64_en_EN.properties详情如下: F60_G00_M100=Please select data. F60_G00_M101=Are yo ...

  10. Linux SSH隧道技术(端口转发,socket代理)

    动态转发(SOCKS5代理): 命令格式:ssh -D <local port> <SSH Server> ssh -fnND 0.0.0.0:20058 172.16.50. ...