intel processor unicode下strlen函数返回值出错原因

We all know how to find the length of a string. For example, if we have defined a pointer to a character string like so:

 

char * pc = "Hello!" ;

we can call

 

iLength = strlen (pc) ;

The variable iLength will be set equal to 6, the number of characters in the string.

Excellent! Now let's try defining a pointer to a string of wide characters:

 

wchar_t * pw = L"Hello!" ;

And now we call strlen again:

 

iLength = strlen (pw) ;

Now the troubles begin. First, the C compiler gives you a warning message, probably something along the lines of

 

`function' : incompatible types - from `unsigned short *' to `const char *'

It's telling you that the strlen function is declared as accepting a pointer to a char, and it's getting a pointer to an unsigned short. You can still compile and run the program, but you'll find that iLength is set to 1. What happened?

The 6 characters of the character string "Hello!" have the 16-bit values:

 

0x0048 0x0065 0x006C 0x006C 0x006F 0x0021

which are stored in memory by Intel processors like so:

 

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

The strlen function, assuming that it's attempting to find the length of a string of characters, counts the first byte as a character but then assumes that the second byte is a zero byte denoting the end of the string.

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值