Crossapp of the VS2013 can not output Chinese, garbled, additional source (a)

Source: Internet
Author: User

VS2013 can be used with iconv, or it can be handled in a wide-byte manner.

Code is the king, directly posted ...

  utf8togbk.h//  utf8togbk////  Created by kevin.//#pragma once#include <iostream> #include < Stdio.h>//string is a C + + header file that contains a string class, and string S1 is the object that creates a string class #include <string>//cstring.h is corresponding to the old C STD version of header file # if (Cc_target_platform = = cc_platform_win32)   #include <cstringt.h> #else   #include "CString" # Endif#include <locale> #define Log_new_line printf ("\ n") using namespace Std;class UTF8TOGBK{PUBLIC:UTF8TOGBK ( void), ~utf8togbk (void),//Convert a Unicode-encoded string to wstringstatic wstring stringtowstring (const string text);// Converts the UTF8 format encoding to the default encoding format of gbk,vs2010 static string TRANSFERTOGBK (const string text);};


utf8togbk.cpp//utf8togbk////Created by kevin.//#include "UTF8ToGBK.h" utf8togbk::utf8togbk (void) {}utf8togbk::~ UTF8TOGBK (void) {}//Converts a string to wstringwstring utf8togbk::stringtowstring (const string str) {setlocale (Lc_all, "CHS"); Const char* _source = Str.c_str (); size_t _dsize = str.size () + 1;wchar_t *_dest = new Wchar_t[_dsize];wmemset (_Dest, 0, _D size); mbstowcs (_dest,_source,_dsize); std::wstring result = _dest;delete []_dest;setlocale (Lc_all, "C"); return result ;} When under the WIN32 platform, convert the UTF8 format encoding to GBK,VS2010 's default encoding format string UTF8TOGBK::TRANSFERTOGBK (const string text) {#if (cc_target_ PLATFORM = = cc_platform_win32) wstring tes = stringtowstring (text); int ascisize = WideCharToMultiByte (cp_utf8,0,tes.c_ STR (), tes.size (), null,0,null,null); if (ascisize = = Error_no_unicode_translation | | ascisize = 0) {return string ();} Char *resultstring = new Char[ascisize];int Converesult = WideCharToMultiByte (Cp_utf8,0,tes.c_str (), Tes.size (), Resultstring,ascisize,null,null); if (converesult! = ascisize) {return striNg ();} String buffer = ""; Buffer.append (resultstring,ascisize);d elete[] Resultstring;return buffer; #elsereturn text;# Endifreturn text;}


Okk, test code as follows, take a key to create a project to do a demonstration.


void Firstviewcontroller::viewdidload () {//Do any additional setup after loading the view from it s nib.    Ccrect winrect = This->getview ()->getbounds ();    caimageview* ImageView = Caimageview::createwithimage (Caimage::create ("helloworld.png"));    Imageview->setframe (Winrect);        This->getview ()->addsubview (ImageView); calabel* label = Calabel::createwithcenter (Ccrect (winrect.size.width*0.5, winrect.size.height*0.5-270,    WinRect.size.width, 200));    Label->settextalignment (Catextalignmentcenter);    Label->setverticaltextalignmet (Caverticaltextalignmentcenter); Label->setfontsize (Crossapp_adptation_ratio); Label->settext (UTF8TOGBK::TRANSFERTOGBK ("Hello!    "). C_str ());    Label->setcolor (Cacolor_white); This->getview ()->insertsubview (label, 1), for (int i = 0; i < 100;i++) {Cclog ("%s:%d", UTF8TOGBK::TRANSFERTOGBK ( "It's a test, kid's shoe!" "). C_str (), i);}}














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.