这是网上的一段源码,能够将一个汉语字符串转换成拼音的首字母。
1 #include <iostream> 2 #include <string> 3 using namespace std; 4 5 static char convert(wchar_t n); 6 static bool In(wchar_t start, wchar_t end, wchar_t code); 7 8 int main(int argc, char* argv[]) 9 { 10 string sChinese = "我爱中国"; // 输入的字符串 11 12 char chr[3]; 13 wchar_t wchr = 0; 14 15 char* buff = new char[sChinese.length()/2]; 16 memset(buff, 0x00, sizeof(char)*sChinese.length()/2+1); 17 18 for (int i = 0, j = 0; i < (sChinese.length()/2); ++i) 19 { 20 memset(chr, 0x00, sizeof(chr)); 21 chr[0] = sChinese[j++]; 22 chr[1] = sChinese[j++]; 23 chr[2] = '/0'; 24 25 // 单个字符的编码 如:'我' = 0xced2 26 wchr = 0; 27 wchr = (chr[0] & 0xff) << 8; 28 wchr |= (chr[1] & 0xff); 29 30 buff[i] = convert(wchr); 31 } 32 33 cout << "pin yin = [" << buff << "]" << endl; 34 system("pause"); 35 return 0; 36 } 37 38 char convert(wchar_t n) 39 { 40 if (In(0xB0A1,0xB0C4,n)) return 'a'; 41 if (In(0XB0C5,0XB2C0,n)) return 'b'; 42 if (In(0xB2C1,0xB4ED,n)) return 'c'; 43 if (In(0xB4EE,0xB6E9,n)) return 'd'; 44 if (In(0xB6EA,0xB7A1,n)) return 'e'; 45 if (In(0xB7A2,0xB8c0,n)) return 'f'; 46 if (In(0xB8C1,0xB9FD,n)) return 'g'; 47 if (In(0xB9FE,0xBBF6,n)) return 'h'; 48 if (In(0xBBF7,0xBFA5,n)) return 'j'; 49 if (In(0xBFA6,0xC0AB,n)) return 'k'; 50 if (In(0xC0AC,0xC2E7,n)) return 'l'; 51 if (In(0xC2E8,0xC4C2,n)) return 'm'; 52 if (In(0xC4C3,0xC5B5,n)) return 'n'; 53 if (In(0xC5B6,0xC5BD,n)) return 'o'; 54 if (In(0xC5BE,0xC6D9,n)) return 'p'; 55 if (In(0xC6DA,0xC8BA,n)) return 'q'; 56 if (In(0xC8BB,0xC8F5,n)) return 'r'; 57 if (In(0xC8F6,0xCBF0,n)) return 's'; 58 if (In(0xCBFA,0xCDD9,n)) return 't'; 59 if (In(0xCDDA,0xCEF3,n)) return 'w'; 60 if (In(0xCEF4,0xD188,n)) return 'x'; 61 if (In(0xD1B9,0xD4D0,n)) return 'y'; 62 if (In(0xD4D1,0xD7F9,n)) return 'z'; 63 return '/0'; 64 } 65 66 bool In(wchar_t start, wchar_t end, wchar_t code) 67 { 68 if (code >= start && code <= end) 69 { 70 return true; 71 } 72 return false; 73 }
但是这种方法不能解决汉语和英语字符混合出现的情况,只需要加一个简单的判断即可。
判断当前字符的ASCII码范围是否符合英语的标准。
实现方法如下
string CPasswordDlg::getShortName(string name) { string shortName; for(int j = 0; j < name.length(); j++) { char m = name[j]; if(name[j] > 47 && name[j] < 123) { shortName += name[j]; } else { char chr[3]; wchar_t wchr = 0; memset(chr, 0x00, sizeof(chr)); chr[0] = name[j++]; chr[1] = name[j]; chr[2] = '/0'; wchr = 0; wchr = (chr[0] & 0xff) << 8; wchr |= (chr[1] & 0xff); shortName += convert(wchr); } } return shortName; }
1 char CPasswordDlg::convert(wchar_t n) 2 { 3 if (In(0xB0A1,0xB0C4,n)) return 'a'; 4 if (In(0XB0C5,0XB2C0,n)) return 'b'; 5 if (In(0xB2C1,0xB4ED,n)) return 'c'; 6 if (In(0xB4EE,0xB6E9,n)) return 'd'; 7 if (In(0xB6EA,0xB7A1,n)) return 'e'; 8 if (In(0xB7A2,0xB8c0,n)) return 'f'; 9 if (In(0xB8C1,0xB9FD,n)) return 'g'; 10 if (In(0xB9FE,0xBBF6,n)) return 'h'; 11 if (In(0xBBF7,0xBFA5,n)) return 'j'; 12 if (In(0xBFA6,0xC0AB,n)) return 'k'; 13 if (In(0xC0AC,0xC2E7,n)) return 'l'; 14 if (In(0xC2E8,0xC4C2,n)) return 'm'; 15 if (In(0xC4C3,0xC5B5,n)) return 'n'; 16 if (In(0xC5B6,0xC5BD,n)) return 'o'; 17 if (In(0xC5BE,0xC6D9,n)) return 'p'; 18 if (In(0xC6DA,0xC8BA,n)) return 'q'; 19 if (In(0xC8BB,0xC8F5,n)) return 'r'; 20 if (In(0xC8F6,0xCBF0,n)) return 's'; 21 if (In(0xCBFA,0xCDD9,n)) return 't'; 22 if (In(0xCDDA,0xCEF3,n)) return 'w'; 23 if (In(0xCEF4,0xD188,n)) return 'x'; 24 if (In(0xD1B9,0xD4D0,n)) return 'y'; 25 if (In(0xD4D1,0xD7F9,n)) return 'z'; 26 return '/0'; 27 }
1 bool CPasswordDlg::In(wchar_t start, wchar_t end, wchar_t code) 2 { 3 if (code >= start && code <= end) 4 { 5 return true; 6 } 7 return false; 8 }