linux 下 Linux 下char转换为wchar_t

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.

 1 #include <string.h>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <wchar.h>
 5 #include <locale.h>
 6 #include <iostream>
 7 using namespace std;
10 // 将char类型转化为wchar13 // locale: 环境变量的值,mbstowcs依赖此值来判断src的编码方式11 int ToWchar(char* &src, wchar_t* &dest, const char *locale = "zh_CN.utf8"){
18   if (src == NULL) {
19     dest = NULL;
20     return 0;
21   }
23   //根据环境变量设置locale
24   setlocale(LC_CTYPE, locale);
26   //得到转化为需要的宽字符大小
27   int w_size = mbstowcs(NULL, src, 0) + 1;
29   //w_size=0说明mbstowcs返回值为-1。即在运行过程中遇到了非法字符(很有可能使locale没有设置正确)
31   if (w_size == 0) {
32     dest = NULL;
33     return -1;
34   }
36   wcout << "w_size" << w_size << endl;
37   dest = new wchar_t[w_size];
38   if (!dest) return -1;
42   int ret = mbstowcs(dest, src, strlen(src)+1);
43   if (ret <= 0)return -1;46   return 0;
47 }
49 int main(){
51   char* str = "中国123";
52   wchar_t *w_str ;
53   ToWchar(str,w_str);
54   wcout << w_str[0] << "--" << w_str[1] << "--" << w_str[2];
55   delete(w_str);
56   return 0;
57 }
 1 #include <stdio.h>
 2
 3 int main(void){
 5        int       i_number, result;
 6        float     f_number;
 7        char      c_number, str[81];
 8        wchar_t   wc_str, ws_str[81];
10        printf( "\n\nEnter an int, a float, two chars and two strings\n");
12        result = scanf( "%d %f %c %C %s %S",                               &i_number, &f_number, &c_number, &wc_str, str, ws_str );
13        printf( "\nThe number of fields input is %d\n",                                result );
14        printf( "The contents are: %d %f %c %C %s %S\n",                              i_number, f_number, c_number, wc_str, str, ws_str);
16        wprintf( L"\n\nEnter an int, a float, two chars and two strings\n");
18        result = wscanf( L"%d %f %hc %lc %S %ls",                               &i_number, &f_number, &c_number, &wc_str, str, ws_str );
19        wprintf( L"\nThe number of fields input is %d\n",                                result );
20        wprintf( L"The contents are: %d %f %C %c %hs %s\n",                               i_number, f_number, c_number, wc_str, str, ws_str);
21 }  
时间: 2024-08-27 08:25:15

linux 下 Linux 下char转换为wchar_t的相关文章

CString 转换为 wchar_t *

1.将CString转换为const char* CString str = _T("231222"); std::string strDp = CStringA(str);  //或: std::string strDp = CT2A(str, CP_ACP); 2.将const char*转换为wchar_t*类型 size_t len = strDp.length() + 1; size_t converted = 0; wchar_t * WStr = (wchar_t*)ma

linux网络环境下socket套接字编程(UDP文件传输)

今天我们来介绍一下在linux网络环境下使用socket套接字实现两个进程下文件的上传,下载,和退出操作! 在socket套接字编程中,我们当然可以基于TCP的传输协议来进行传输,但是在文件的传输中,如果我们使用TCP传输,会造成传输速度较慢的情况,所以我们在进行文件传输的过程中,最好要使用UDP传输. 在其中,我们需要写两个程序,一个客户端,一个服务端,在一个终端中,先运行服务端,在运行客户端,在服务端和客户端都输入IP地址和端口号,注意服务端和客户端的端口号要相同,然后选择功能,在linux

centos下Linux C语言MD5的使用

在Linux C变成中用到MD5加密会使用到openssl库,下面给出的是一个简单的小Demo: #include <stdio.h> #include <openssl/md5.h> #include <string.h> #define MD5_LENGTH 16 #define MAX 40 int main(void) { MD5_CTX ctx; unsigned char data[MAX]; unsigned char md[MD5_LENGTH]; ch

Linux文本模式下监听鼠标事件

Linux文本模式下监听鼠标事件,这里是通过gpm这个来实现的,代码如下: 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 #include <gpm.h> 5 6 void main(int argc,char **argv) 7 { 8 fd_set readset; 9 Gpm_Event event; 10 Gpm_Connect conn; 11 12 conn.

Linux 0.11下信号量的实现和应用

Linux 011下信号量的实现和应用 生产者-消费者问题 实现信号量 信号量的代码实现 关于sem_wait和sem_post sem_wait和sem_post函数的代码实现 信号量的完整代码 实现信号量的系统调用 测试用的应用程序的实现 Linux 0.11下信号量的实现和应用 1.生产者-消费者问题 从一个实际的问题:生产者与消费者出发,谈一谈为什么要有信号量?信号量用来做什么? 问题描述:现在存在一个文件”.\buffer.txt”作为一个共享缓冲区,缓冲区同时最多只能保存10个数.现

Linux多线程编程和Linux 2.6下的NPTL

Linux多线程编程和Linux 2.6下的NPTL 在Linux 上,从内核角度而言,基本没有什么线程和进程的区别--大家都是进程.一个进程的多个线程只是多个特殊的进程他们虽然有各自的进程描述结构,却共享了同一 个代码上下文.在Linux上,这样的进程称为轻量级进程Light weight process.致此,就是关于线程的总体概念了,我们往往就在了解这个概念的情况下开始我们的多线程编程之旅.这对于多线程编程入门已经足够了,然而事 实上线程却要复杂的多. 首先多线程间的优先级调度,内存资源(

Centos 7.3下 Linux For SQL Server安装及配置介绍

Centos 7.3下Linux For SQL Server安装及配置介绍 说到SQL Server服务,我们大家都知道是Microsoft公司的数据库服务,当然说到数据库,现在主要分为三大商:1:Oracle.2:Msql Server.3:Mysql:三种数据库在当下环境受到不了不同程度的关注:比如oracle主要应用到大型的商业比较多,比如银行:SQL Server主要在常见的互联网公司使用:mysql主要应用于小型的企业或者服务商使用:当然从费用上来说,Oracle是最贵的,也是最为稳

linux获取目录下文件

查看当前目录下的文件:find . -type f查看当前目录下的文件夹: find . -type d如果文件file1不为空: if [ -s file1 ];then       echo "file1 不为空"fi #!/bin/sh for f in `find ./testdir -type f`; do         if [ -s $f ];then                 echo $f is not empty.                 echo 

linux mini模式下如何制作本地yum源,并用远程工具安装 vim

linux mini模式下如何制作本地yum源,并用远程工具安装 vim 打开虚拟机 启动centos 系统 点右下角的光盘图标 出现连接(或是断开).设置,下拉菜单,选择 "设置" 在虚拟机设置对话框内如图所示 [[email protected] ~]# vim -bash: /usr/bin/vim: 没有那个文件或目录 光盘挂载: 查看系统中所有的挂载信息 [[email protected] ~]# mount /dev/sda3 on / type ext4 (rw) pr