汉字截断问题

孤人 提交于 2019-11-30 00:24:19
//判断汉字截断
#include <windows.h>
#include <iostream>
#include <string>
using namespace std;
#pragma comment(lib,"user32.lib")

bool IsHanZiCorrupted( string &SeriesRecvString )
{
       DWORD len=strlen( SeriesRecvString.c_str() );
       DWORD i=0;
        while( i<len)
        {	
              
                if(  IsDBCSLeadByte( SeriesRecvString[i] ) )
                   {      
                        i+=2;
                        if( i> len )
                        {
                                 return true;
                        }
                }
                else
                {
                        ++i;
                }
        }
        return false;
}

int main ()
{
	//0x81:汉字内码扩展规范(GBK) 1stByte :0x81~0xFE; 2ndByte:0x40~0x7E;
	//if( (unsigned char)SeriesRecvString[i]>=0x81 ) 一定要转成无符号数才能正确比较
		
		string str("c鑯d苦e命f三g国h无i双j楚k王l好m细n腰o宫p中q多r饿s死敌");

	 
		cout<<"str=\""<<str<<"\""<<endl;
		cout<<"len()="<<str.length()<<endl;
		cout<<"size()="<<str.size()<<endl;
		cout<<"strlen(c_str())="<<strlen( str.c_str() )<<endl<<endl;
		
		for(int i=0;i<str.length();++i)
		{
			printf("str[%d]:%02x",i,(unsigned char) str[i]);
			if( IsDBCSLeadByte(str[i]) )cout<<":DBCS ";
				else cout<<":"<<str[i];
			cout<<endl;
		} 
		IsHanZiCorrupted(str)?cout<<"IsHanZiCorrupt:yes"<<endl:cout<<"IsHanZiCorrupt:no"<<endl;
		cout<<endl;

		str[51]='a';
		for(int i=0;i<str.length();++i)
		{
			printf("str[%d]:%02x",i,(unsigned char) str[i]);
			if( IsDBCSLeadByte(str[i]) )cout<<":DBCS ";
				else cout<<":"<<str[i];
			cout<<endl;
		} 
		IsHanZiCorrupted(str)?cout<<"IsHanZiCorrupt:yes"<<endl:cout<<"IsHanZiCorrupt:no"<<endl;


} 
///////////////////
str="c鑯d苦e命f三g国h无i双j楚k王l好m细n腰o宫p中q多r饿s死敌"
len()=53
size()=53
strlen(c_str())=53


str[0]:63:c
str[1]:e8:DBCS
str[2]:7e:~
str[3]:64:d
str[4]:bf:DBCS
str[5]:e0:DBCS
str[6]:65:e
str[7]:c3:DBCS
str[8]:fc:DBCS
str[9]:66:f
str[10]:c8:DBCS
str[11]:fd:DBCS
str[12]:67:g
str[13]:b9:DBCS
str[14]:fa:DBCS
str[15]:68:h
str[16]:ce:DBCS
str[17]:de:DBCS
str[18]:69:i
str[19]:cb:DBCS
str[20]:ab:DBCS
str[21]:6a:j
str[22]:b3:DBCS
str[23]:fe:DBCS
str[24]:6b:k
str[25]:cd:DBCS
str[26]:f5:DBCS
str[27]:6c:l
str[28]:ba:DBCS
str[29]:c3:DBCS
str[30]:6d:m
str[31]:cf:DBCS
str[32]:b8:DBCS
str[33]:6e:n
str[34]:d1:DBCS
str[35]:fc:DBCS
str[36]:6f:o
str[37]:b9:DBCS
str[38]:ac:DBCS
str[39]:70:p
str[40]:d6:DBCS
str[41]:d0:DBCS
str[42]:71:q
str[43]:b6:DBCS
str[44]:e0:DBCS
str[45]:72:r
str[46]:b6:DBCS
str[47]:f6:DBCS
str[48]:73:s
str[49]:cb:DBCS
str[50]:c0:DBCS
str[51]:b5:DBCS
str[52]:d0:DBCS
IsHanZiCorrupt:no


str[0]:63:c
str[1]:e8:DBCS
str[2]:7e:~
str[3]:64:d
str[4]:bf:DBCS
str[5]:e0:DBCS
str[6]:65:e
str[7]:c3:DBCS
str[8]:fc:DBCS
str[9]:66:f
str[10]:c8:DBCS
str[11]:fd:DBCS
str[12]:67:g
str[13]:b9:DBCS
str[14]:fa:DBCS
str[15]:68:h
str[16]:ce:DBCS
str[17]:de:DBCS
str[18]:69:i
str[19]:cb:DBCS
str[20]:ab:DBCS
str[21]:6a:j
str[22]:b3:DBCS
str[23]:fe:DBCS
str[24]:6b:k
str[25]:cd:DBCS
str[26]:f5:DBCS
str[27]:6c:l
str[28]:ba:DBCS
str[29]:c3:DBCS
str[30]:6d:m
str[31]:cf:DBCS
str[32]:b8:DBCS
str[33]:6e:n
str[34]:d1:DBCS
str[35]:fc:DBCS
str[36]:6f:o
str[37]:b9:DBCS
str[38]:ac:DBCS
str[39]:70:p
str[40]:d6:DBCS
str[41]:d0:DBCS
str[42]:71:q
str[43]:b6:DBCS
str[44]:e0:DBCS
str[45]:72:r
str[46]:b6:DBCS
str[47]:f6:DBCS
str[48]:73:s
str[49]:cb:DBCS
str[50]:c0:DBCS
str[51]:61:a
str[52]:d0:DBCS
IsHanZiCorrupt:yes


转载于:https://my.oschina.net/u/160145/blog/264398

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!