#include <iostream> #include <string.h> #include <stdio.h> #include <stdlib.h> using namespace std; /***linux下一个中文占用三个字节,windows占两个字节***/ void chinese_or_english(char *str) { char chinese[4] = {0}; for (int i = 0; i < strlen(str); i++) { //if (str[i] >= 0 && str[i] <= 127) { //ascII if ((str[i] & 0x80) == 0) { //chinese:the top is 1 cout<<"alpha:"<<str[i]<<endl; } //else if (str[i] < 0){ else { chinese[0] = str[i]; chinese[1] = str[i + 1]; chinese[2] = str[i + 2]; i++; //skip one more i++; printf("chinese:%s\n", chinese); } } } int main() { char str[] = "tai太阳yang"; cout<<strlen(str)<<endl; chinese_or_english(str); return 0; }
判断占用字节数。
int SVPSettingKeyboard::chinese_or_english(std::string str,int index) { if(index > str.size()-1) return 0; if((str[index-1]& 0x80) == 0){ return 1; }else{ int n = 2; while(((str[index-n] &0x80) == 0x80 )&&((str[index-n] &0x40) == 0)){ ++n; } return n; } }