Linux below Linux convert char to wchar_t

Source: Internet
Author: User
Tags locale

Use the MBSTOWCS function under Linux to convert char to wchar_t
function meaning: Convert a multibyte string to a wide char string
Description: The behaviour of mbstowcs depends on the lc_ctype category of the current locale
Return value: the MBSTOWCS () function returns the number of wide characters that make up the converted part of the Wide-char-act  Er string, not including the terminating null wide character. If an invalid multibyte sequence were 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 namespacestd;Ten //Convert char type to WCHAR - //locale: The value of the environment variable, MBSTOWCS relies on this value to determine how the SRC is encoded
intTowchar (Char* &AMP;SRC, wchar_t* &dest,Const Char*locale ="Zh_cn.utf8"){ - if(src = =NULL) { +Dest =NULL; - return 0; + } at //setting locale based on environment variables - setlocale (lc_ctype, locale); - //get converted to the desired wide character size - intW_size = mbstowcs (NULL, SRC,0) +1; in //w_size=0 Description The Mbstowcs return value is-1. Illegal characters are encountered during operation (it is likely that the localenot set correctly) to if(W_size = =0) { +Dest =NULL; - return-1; the } $Wcout <<"w_size"<< w_size <<Endl;Panax NotoginsengDest =NewWchar_t[w_size]; - if(!dest)return-1; the intret = mbstowcs (dest, SRC, strlen (SRC) +1); + if(Ret <=0)return-1; $ return 0; - } the intMain (){Wuyi Char* str ="China 123"; thewchar_t *W_str; - Towchar (STR,W_STR); WuWcout << w_str[0] <<"--"<< w_str[1] <<"--"<< w_str[2]; - Delete(W_STR); About return 0; $}

1#include <stdio.h>2   3 intMainvoid){  5        intI_number, result; 6        floatF_number; 7        CharC_number, str[Bayi]; 8wchar_t Wc_str, ws_str[Bayi]; Tenprintf"\n\nenter an int, a float, chars and both strings\n");  Aresult = 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 is:%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, chars and both 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 is:%d%f%c%c%hs%s\n",
I_number, F_number, C_number, Wc_str, str, WS_STR); +}

Linux below Linux convert char to wchar_t

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.