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.
注意:wcout 与cout不要混合使用,否则会导致wchar_t的输出问题

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <wchar.h>
#include <locale.h>
#include <iostream>
using namespace std;
 
 
// 将char类型转化为wchar
// src:  源
// dest: 目标
// locale: 环境变量的值,mbstowcs依赖此值来判断src的编码方式
// 运行成功返回0 否则返回-1
//
int ToWchar(char* &src, wchar_t* &dest, const char *locale = "zh_CN.utf8")
{
  if (src == NULL) {
    dest = NULL;
    return 0;
  }
 
  // 根据环境变量设置locale
  setlocale(LC_CTYPE, locale);
 
  // 得到转化为需要的宽字符大小
  int w_size = mbstowcs(NULL, src, 0) + 1;
 
  // w_size = 0 说明mbstowcs返回值为-1。即在运行过程中遇到了非法字符(很有可能使locale
  // 没有设置正确)
  if (w_size == 0) {
    dest = NULL;
    return -1;
  }
 
  wcout << "w_size" << w_size << endl;
  dest = new wchar_t[w_size];
  if (!dest) {
      return -1;
  }
 
  int ret = mbstowcs(dest, src, strlen(src)+1);
  if (ret <= 0) {
    return -1;
  }
  return 0;
}
 
int main()
{
  char* str = "中国123";
  wchar_t *w_str ;
  ToWchar(str,w_str);
  wcout << w_str[0] << "--" << w_str[1] << "--" << w_str[2];
  delete(w_str);
  return 0;
}

————————————————
原文链接:https://blog.csdn.net/caigen1988/article/details/19075073

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值