怎么识别是一中文汉字还是两个字符呢?

gggjjj 2006-11-01 08:42:17
一个中文汉字在C中占两个字节,一个西文字符占一个字节,那么怎么识别16位的一个串是代表一个中文字符呢还是两个西文字符呢?
...全文
160 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
sanniko 2006-11-01
  • 打赏
  • 举报
回复
[DllImport("kernel32")]
private static extern int lstrlenA(string lpString);
Jorge-Merodio 2006-11-01
  • 打赏
  • 举报
回复
现在英文也是两个了
jxhwei 2006-11-01
  • 打赏
  • 举报
回复
Unicode的特点是:
不管哪一国的字符码均以两个Byte表示,例如"A"在Unicode则是16进制41和00的组合,即4100,高位41(转换为Ascii码即是65=A)

如果用的是Ascii码,自然不存在这个问题,如果是Unicode码,那么,如果后2位是00的,就是英文字符
gggjjj 2006-11-01
  • 打赏
  • 举报
回复
最后一句有些看不懂啊。
Knight94 2006-11-01
  • 打赏
  • 举报
回复
通过Encoding来判断,例如:
private int GetChsCharLength( string strValue )
{
if( strValue == null || strValue == "" ) return 0;

Encoding encGB = Encoding.GetEncoding( "gb2312" );
Encoding encUnicode = Encoding.Unicode;

return strValue.Length - ( encUnicode.GetByteCount( strValue ) - encGB.GetByteCount( strValue ) );
}

private int GetAniCharLength( string strValue )
{
if( strValue == null || strValue == "" ) return 0;

Encoding encGB = Encoding.GetEncoding( "gb2312" );
Encoding encUnicode = Encoding.Unicode;

return encUnicode.GetByteCount( strValue ) - encGB.GetByteCount( strValue );
}
相关推荐
发帖
C#

10.9w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2006-11-01 08:42
社区公告

让您成为最强悍的C#开发者