Into the world of Windows programming-----character encoding

Source: Internet
Author: User

1 character encoding 1.1 encoded history 1.1.1ASCII code

0=127 7-bit representation

1.1.2ASCII Extension Code

0-255 8 for the expression.

Code page: Toggle the corresponding character by code (numeric representation)

1.1.3 Double-byte character set DBCS

Use one or two bytes to represent the character.


1.1.4Unicode encoding

Use all 2 bytes to represent a character


Memory hard drives, and other resource usage are getting larger. High support for encoding.

Character

1.2C language and encoding 1.2.1 single-byte characters and strings

Char ctext = ' A ';

Char * PszText = "ABCD"

void ASCII () {Char ctext = 0;for (int nindex = 0; nindex <; nindex + +) {printf ("%c", ctext); Ctext + +;} Puts ("");} void C_char () {char *psztext = "Hello worlf\n"; int nlen = strlen (psztext);p rintf ("Nlen =%d,pszchar =%s", nlen,psztext);}
void coadpage (int ncodepage) {setconsoleoutputcp (ncodepage); char ctext = 0;for (int nindex = 0; nindex <; nindex + +) {printf ("%c", ctext); Ctext + +;} Puts ("");}



1.2.2 Wide-byte characters

wchar_t ctext = ' A ';

wchar_t *psztext= L "ABCD"

void C_wchar () {wchar_t ctext = ' A '; wchar_t *psztext = L "ABCD";/* double-byte uncode character */int Nlen = Wcslen (PszText);/* can only print A to, cannot print full * /printf ("%s\n", pszText);/* printable complete string */wprintf (L "%s,len =%d\n", psztext,nlen);/* Multibyte character */char * Pszchs = "I am a programmer"; Nlen = Strlen (Pszchs);p rintf ("%s,len=%d", Pszchs,nlen);}


1.2.3 Defining Universal Character functions

For the program can easily support wide-byte and multibyte characters, using TCHAR to define, the basic implementation of the following ideas.

#ifndef _unicode

typedef char TCHAR

#define _T (x) x

#else

typedefwchar_t TCHAR

#define _T (x) l# #x//## even ¢? Connect ¨® character ¤?

#endif

void Tchat () {TCHAR * PszText = __t ("I am a programmer"), #ifndef _unicodeint Nlen = strlen (pszText); #elseint Nlen = Wcslen (pszText); #en Dif
1.1.1Unicode Console Printing

Outputs a Unicode string to the console. Reference: http://blog.sina.com.cn/s/blog_4e3197f20100a7v3.html

Similarly,Windowsthe console is also standardUNICODEsystem functions, there are alsoXxxa,Xxxwtwo ways to manipulate it.Win32 API, below toWriteconsoleas an example. It also existsWriteconsoleaand theWriteconsolewtwo on the version, but we're inMSDNThe two prototypes cannot be found in theC:\ProgramFiles\Microsoft Visual Studio\vc98\include\wincon. HThe definitions can be found and predefined statements that control them can be found.

#ifdefUNICODE

#defineWriteConsole Writeconsolew

#else

#defineWriteConsole Writeconsolea

#endif

void Printunicode () {/* Sets the parameter to *std_input_handle (standard input handle) *std_ouput_handle (standard output handle) as required for the handle type to be obtained *std_error_handle ( Standard error handle), *  function return value. The handle value. */handle HOut = GetStdHandle (std_output_handle); wchar_t * PszText = l "I am a programmer \ n";/* *bool WINAPI writeconsole (*handle hcons Oleoutput, the console handle, should be the standard output handle * *const void* lpbuffer, the output of the contents of the pointer * *DOWRD Nnumberofcharstowrite, the number of characters output * *DOWRD lpnumberofch Arswritten output parameter, which returns the actual output of the number of characters * *lpvoid lpreserved); reserved arguments, must be set to NULL */writeconsolew (Hout,psztext,wcslen (pszText), NULL , NULL);}


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.