Mutual conversion of strings (full)

Source: Internet
Author: User
Tags uppercase character
 Wchar_t and wstring type output problems
How can I output a string of the wchar_t and wstring types? It cannot be output by cout. I cannot use wcout to compile the string.

DEV-C ++ environment.
# Include <iostream>
# Include <conio. h>
# Include <string>

Using namespace STD;

Int main ()
{
Char S1 [] = "char ";
Wchar_t S2 [] = l "wchar_t ";
// Wstring Ws = l "wstring ";

Cout <"S1 =" <S1 <Endl;
Cout <"S2 =" <S2 <Endl; // The output address is
// Cout <ws <Endl; // compilation error message


Getch ();
Return 0;
}

Reply content

[Winwing ]:
Wcout is not supported by every platform -_-

[Jixingzhong ]:
Wcout in VC Environment

[Jixingzhong ]:
Wchar_t: Use the corresponding wide character version to operate the function.

[Reference
For example, the function used to evaluate the length of a wide string is
Size_t _ cdel wchlen (const wchar_t *);
Why do we need to define these functions? The most fundamental reason is that all strings in ANSI are identified by '\ 0' at the end of the string (the Unicode string ends with "\ 0 \ 0 ), the correct operations on many string functions are based on this. However, we know that a character occupies one space in the memory when it is a wide character, which will make the string function that operates the ANSI character unable to operate correctly. Take the "hello" string as an example. The following five characters are contained in the string:
0x0048 0x0065 0x006c 0x006c 0x006f
In the memory, the actual arrangement is:

48 00 65 00 6C 00 6C 00 6f 00
Therefore, when an ANSI string function, such as strlen, encounters the first 00 after 48, it will consider the string to the end, the result of using strlen to evaluate the length of a wide string will always be 1!
]

[Jixingzhong ]:
Comparison between wide character processing functions and common functions

 

Character classification: Description of common C functions of wide character Functions
Iswalnum () isalnum () test whether the character is a number or letter
Iswalpha () isalpha () test whether the character is a letter
Iswcntrl () iscntrl () test whether the character is a controller
Iswdigit () isdigit () test whether the character is a number
Iswgraph () isgraph () test whether the character is visible
Iswlower () islower () test whether the character is a lowercase character
Iswprint () isprint () test whether the character is printable
Iswpunct () ispunct () test whether the character is a punctuation mark
Iswspace () isspace () test whether the character is a blank sign
Iswupper () isupper () test whether the character is an uppercase character
Iswxdigit () isxdigit () test whether the character is a hexadecimal number

Case sensitivity conversion:
Description of common C functions of wide character Functions
Towlower () tolower () converts characters to lowercase letters
Towupper () toupper () converts the character to uppercase

Character comparison: Description of common C functions of wide character Functions
Wcscoll () strcoll () Comparison string

Date and Time conversion:
Wide character Function Description
Strftime () sets the format date and time based on the specified string format and locale
Wcsftime () sets the format date and time based on the specified string format and locale, and returns the wide string
Strptime () converts a string to a time value based on the specified format, which is the reverse process of strftime.

Print and scan strings:
Wide character Function Description
Fprintf ()/fwprintf () format the output using the vararg Parameter
Fscanf ()/fwscanf () format read
Printf () uses the vararg parameter to format the output to the standard output.
Scanf () reads formatted data from standard input
Sprintf ()/swprintf () formatted as a string based on the vararg parameter table
Sscanf () is formatted and read as a string
Vfprintf ()/vfwprintf () format the output to a file using the stdarg parameter table
Vprintf () uses the stdarg parameter table to format the output to the standard output.
Vsprintf ()/vswprintf () format the stdarg parameter table and write it to the string

Digital Conversion:
Description of common C functions of wide character Functions
Wcstod () strtodd () converts the initial part of a wide character to a double-precision floating point number.
Wcstol () strtol () converts the initial part of a wide character to a long integer.
Wcstoul () strtoul () converts the initial part of a wide character to an unsigned long integer.

Multi-Byte Character and wide character conversion and operations:
Wide character Function Description
Mblen () determines the number of characters in bytes based on locale settings
Mbstowcs () converts a multi-byte string to a wide string.
Mbtowc ()/btowc () converts multi-byte characters into wide characters
Wcstombs () converts a wide string to a multi-byte string.
Wctomb ()/wctob () converts a wide character to a multi-Byte Character

Input and Output:
Description of common C functions of wide character Functions
Fgetwc () fgetc () reads a character from the stream and converts it to a wide character.
Fgetws () fgets () reads a string from the stream and converts it to a wide string.
Fputwc () fputc () converts wide characters into multi-byte characters and outputs them to standard output.
Fputws () fputs () converts a wide string into multiple bytes and outputs the string to the standard output string.
Getwc () GETC () reads characters from the standard input and converts them to wide characters.
Getwchar () getchar () reads characters from the standard input and converts them to wide characters.
None gets () Use fgetws ()
Putwc () putc () converts a wide character into a multi-Byte Character and writes it to the standard output.
Putwchar () putchar () converts a wide character into a multi-Byte Character and writes it to the standard output.
None puts () Use fputws ()
Ungetwc () ungetc () places a wide character in the input stream

String operation:
Description of common C functions of wide character Functions
Wcscat () strcat () concatenates a string to the end of another string
Wcsncat () strncat () is similar to wcscat () and specifies the bond length of the bond string.
Wcschr () strchr () finds the first position of the substring
Wcsrchr () strrchr () searches for the first position of the substring from the tail.
Wcspbrk () strpbrk () Searches from the character string for the first occurrence of any character in the other string
Wcswcs ()/wcsstr () strchr () searches for the first occurrence location of another string in one string
Returns the initial number of records that do not contain the second string.
Wcsspns () strspns () returns the initial number of strings containing the second string.
Wcscpy () strcpy () Copy string
Wcsncpy () strncpy () is similar to wcscpy () and specifies the number of copies.
Wcscmp () strcmp () compares two wide strings
Wcsncmp () strncmp () is similar to wcscmp () and also specifies the number of character strings to be compared.
Wcslen () strlen () gets the number of wide strings
Wcstok () strtok () Splits a wide string into a series of strings Based on the identifier
Wcswidth () None to get the width of the wide string
Wcwidth () None

In addition, wmemcpy (), wmemchr (), wmemcmp (), wmemmove (), and wmemset () for memory operations ().

[Todototry ]:
Mark

[Zlbruce ]:
From: http://blog.vckbase.com/bruceteen/archive/2005/11/15/14924.aspx
Supplement: to use wcout and wstring in mingw32, you need to add some macros, such
# DEFINE _ glibcxx_use_wchar_t 1
# Include <iostream>
Int main (void)
{
STD: wcout <1 <STD: Endl;
}
It can be compiled, but it cannot be linked. Google it on the Internet. stlport said that mingw32 is faulty, and mingw32 said that M $'s C Runtime is faulty.

 

String to cstring
Cstring. Format ("% s", String. c_str ());

 

Convert Char to cstring
Cstring. Format ("% s", char *);

 

Char to string
String S (char *);

 

String to char *
Char * P = string. c_str ();

 

Cstring to string
String S (cstring. getbuffer_r ());
After getbuffer_r (), you must release the buffer space.

Cstring/string mutual conversion int, float

Converts a character to an integer. You can use atoi, _ atoi64, or atol.

To convert a number to a string variable, you can use the ITOA function.

Convert a number to a cstring variable. You can use the format function of cstring, for example:
Cstring S;
Int I = 64;
S. Format ("% d", I );

 

Char * To cstring type

Convert char * To cstring and assign values directly, for example:
Cstring strtest;
Char * charpoint;
Charpoint = "give string a value ";
Strtest = charpoint;

 

You can also use cstring: Format for example:

 Char charray [] = "charTest ";
 Tchar * P = _ T ("charTest "); (or lptstr P = _ T (" charTest ");)
 Cstring thestring = charray;
 Thestring. Format (_ T ("% s"), charray );
 Thestring = P;

 

Cstring is converted to the char * (lpstr) type. The following three methods are often used:

Method 1: use forced conversion. For example:
 Cstring thestring (_ T ("char test "));
 Lptstr lpsz = (lptstr) (lpctstr) thestring;

Method 2: Use strcpy. For example:
 Cstring thestring (_ T ("char test "));
Lptstr lpsz = new tchar [thestring. getlength_r () + 1];
Strcpy (lpsz, thestring );

In addition:
Cstring to Char []
Cstring conversion char [100]
Char A [100];
Cstring STR ("aaaaaa ");
Strncpy (A, (lpctstr) STR, sizeof ());

It should be noted that the second parameter of strcpy (or _ tcscpy of the value to be moved) is const wchar_t * (UNICODE) or const char * (ANSI ), the system compiler will automatically convert it.

Method 3: Use cstring: getbuffer.
If you need to modify the content in cstring, there is a special method that can be used, that is, getbuffer, which is used to return a writable buffer pointer. If you only want to modify characters or truncate strings, for example:
Cstring S (_ T ("char test "));
 Lptstr P = S. getbuffer_r ();
 Lptstr dot = strchr (p ,''.'');
If (P! = NULL)
 * P = _ T ('');
 S. releasebuffer (); // release immediately after use, so that other cstring member functions can be used.
In the range between getbuffer and releasebuffer, you must not use any method of the buffer cstring object you want to operate on. Because the integrity of the cstring object is not guaranteed before releasebuffer is called

 

The last step is to use two API functions: mtow () and wtom ()

Generally, the string conversion problem can be solved.

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.