Unicode and ASCII for Visual Studio

Source: Internet
Author: User

Visual C + + since version 2005, the project's default character set property is changed to use the wide character set (Unicode), so the multibyte character set (ASCII) should not be used by default.

This change allows us to get the code from the Internet, the book is not properly compiled. For example, the following program, which is based on the console and obtains the absolute path to the file.

#include <windows.h>#include<stdio.h>int  main () {   char  Szdir[max_path];    GetCurrentDirectory (max_path,szdir);   printf ("The currentDirectory is%s", szdir);    GetChar ();    return 0 ;}

In the Visual C + + version after 2005, the program did not compile properly because Unicode was used by default.

There are many ways to solve this problem:

Method One: (recommended)

types, functions that use Unicode. In this example, the type char becomes TCHAR, the function printf is changed to wprintf, the string is preceded by an L, or is placed in parentheses in the _t (). As follows:

#include <windows.h>#include<stdio.h>int  main () {   TCHAR szdir[max_path];    GetCurrentDirectory (max_path,szdir);   wprintf (L"The currentDirectory is%s", szdir);    GetChar ();    return 0 ;}

Method Two: (recommended)

Do not modify the source code, but to modify the compiler's settings.

Right-click "Project"-"Properties"-"General"-"character set" (changed from default Unicode character set to "not set" or "multibyte Character set")

Method Three: (Not recommended)

A function that changes the function to ASCII. If the getcurrentdirectory is changed to Getcurrentdirectorya.

In addition, the compiler cannot pass the error message that usually prompts for type conversion. For example, the hint cannot convert parameter 2 from "Char [260]" to "LPWSTR". If the char type string is cast to the LPWSTR type at this point, because each character in Unicode occupies 2 bytes, each character in ASCII occupies one byte, and the printf function outputs only the first character.

Unicode and ASCII for Visual Studio

Related Article

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.