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分区需 ...
随机推荐
- docker 修改 mysql 5.7 sql_mode
docker exec -ti {容器ID} /bin/bash 进入容器 apt-get install vim 安装vim 找到 vim /etc/mysql/my.cnf 在 [mysqld ...
- scala(一)方法&函数
写在前面 众所周知,scala一向宣称自己是面向函数的编程,(java表示不服,我是面向bean的编程!)那什么是函数? 在接触java的时候,有时候用函数来称呼某个method(实在找不出词了),有 ...
- 【cs231n】神经网络学习笔记1
神经网络推荐博客: 深度学习概述 神经网络基础之逻辑回归 神经网络基础之Python与向量化 浅层神经网络 深层神经网络 前言 首先声明,以下内容绝大部分转自知乎智能单元,他们将官方学习笔记进行了很专 ...
- DataReader 连接数据库完整过程和代码(Sql Server)
数据库名叫:Bu 有个表:A 里面有一列:ID 需要引用 using System.Data.SqlClient; 代码部分如下: SqlConnection sqlCon=new SqlConnec ...
- 医疗数据库 Caché 开发笔记
目前所知的 Caché 是应用在医院信息系统(即 HIS),据说在欧美医疗卫生行业,Caché 占了 70% 的市场份额.国内的东华软件就是采用 Caché 数据库,东华软件在国内医院市场占有率大致为 ...
- QtCreator的中如何使用第三方依赖库
> https://blog.csdn.net/e5Max/article/details/9840331 ```LIBS += -L/usr/local/lib -lmath ``` ``` ...
- php http post发送失败的问题
//params 是json对象private function HttpPost($url, $params){$data = http_build_query($params); //写 ...
- LeetCode第[79]题(Java):Word Search(矩阵单词搜索)
题目:矩阵单词搜索 难度:Medium 题目内容: Given a 2D board and a word, find if the word exists in the grid. The word ...
- js三种对话框
<html> <head> <script> function tip1(){ //这个基本没有什么说的,就是弹出一个提醒的对话框 alert("这个第一 ...
- maven笔记(2)
项目管理利器(Maven)——maven的生命周期和插件Maven的生命周期大概如下:clean compile test package install这几个命令对应了一个项目的完整的构建过程,这几 ...