About getting the length of the string

xiaoxiao2021-03-06  40

We all know how to get the length of the string. For example, if we are already like this, a string indicator is defined:

Char * pc = "Hello!";

We can call

Ilength = Strlen (PC);

At this time, the variable ilength will be equal to 6, which is the number of characters in the strings.

Great! Let us now try to define an indicator pointing to wide font:

Wchar_t * pw = l "Hello!";

Call Strlen again:

Ilength = Strlen (PW);

It's time to come now. First, the C compiler displays a warning message, which may be such content:

'Function': Incompatible Types - from 'unsigned short *' to 'const char *'

This message means: When the strlen is, this correspondence should receive a CHAR type indicator, but it is now receiving an indicator of a UNSIGNED SHORT type. You can still compile and execute the program, but you will find that Ilength is equal to 1. why?

The 6 fifth of the string "Hello!" Take up 16-bit yuan:

0x0048 0x0065 0x006c 0x006c 0x006f 0x0021

The Intel processor saves them in memory:

48 00 65 00 6C 00 6C 00 6F 00 21 00

It is assumed that the Strlen is tried to get the length of a string, and the first bit tuple begins to count, but then it is assumed that if the next bit element group is 0, the string ends.

This small practice clearly shows the difference between the C language itself and the execution period library. The compiler will explain the string L "Hello!" As a set of 16-bit short integer data and saved it in the Wchar_t array. The compiler also processes the array index and the SIZEOF operator, so these can work normally, but add execution period libraries, such as Strlen when connecting. These functions consider the string consist of the unit component font. When you encounter a wide string, the letter is not as implemented as we hope.

You may have to say: "Oh, too much trouble!" Now every C language program library must be rewritten to accept wide font. But in fact, it is not that every C language program is rewritten, but those who have string parameters need to be rewritten, and they don't have to be done by you. They have been overwritten.

The wide font version of the strlen is WCSLEN (Wide-Character String Length: Wide-Character String Length), and in String.h (which also illustrates strlen) and Wchar.h have an explanation. The Strlen letter is as follows:

SIZE_T __CDECL STRLEN (const char *);

The WCSLEN is described below:

SIZE_T __CDECL WCSLEN (const wchar_t *);

At this time, we know that you can call the length of the wide string.

ilength = WCSLEN (PW);

The letter will return the number of characters in the string 6. Remember, after changing into a width unit, the character length of the string does not change, just the length of the bit group change.

All of you are familiar with C executive period library in the execution period with a wide font version. For example, WPRINTF is a wide font version of Printf. These functions are illustrated in Wchar.h and a header file containing standard letter instructions.

转载请注明原文地址:https://www.9cbs.com/read-56579.html

New Post(0)