- (NSString*)getPinYin:(NSString *)nsstrHZ { NSString* strPY = @""; char pinyin[6] = {0}; memset(pinyin , 0 , sizeof(pinyin)); char ch = 0; unsigned char high = 0, low = 0; const char* pszHZ = [nsstrHZ cStringUsingEncoding:gbEncoding]; for(int i = 0; i < strlen(pszHZ); i++, low = 0) { high = (unsigned char)(pszHZ[i]); if (high < 128 && high != 32 && high != '*') // 半角字符 { ch = (char)high; if(ch >= 'a' && ch <= 'z') ch -= 32; //Make upper strPY = [strPY stringByAppendingFormat:@"%c", ch]; continue; } if((i+1) < strlen(pszHZ)) low = (unsigned)(pszHZ[i+1]); if (high >= 129 && low > 64 ) { switch(high) { case 163: // 全角 ASCII pinyin[0] = low - 128; // 控制不能输出非数字, 字母的字符 //此条件永远不成立,笔误? //if (pinyin[1] < 48 && pinyin[1] > 57 && pinyin[1] < 65 && pinyin[1] > 90 && pinyin[1] < 97 && pinyin[1] >122) // strcpy(pinyin,""); strPY = [strPY stringByAppendingFormat:@"%s", pinyin]; break; case 162: // 数字 if(low > 160) strcpy(pinyin, PYCharIndex[low - 161]); else strcpy(pinyin, " "); strPY = [strPY stringByAppendingFormat:@"%s", pinyin]; break; default: // 一般汉字, 获得拼音索引 if(183 == high && 240 == low) //佛 { pinyin[0] = 'F'; } else { int index = PYCodeIndex[high-129][low-64]; if (index == 0) strcpy(pinyin, " "); else { const char* code = PYCode[index-1]; pinyin[0] = code[0]; } if (0xB3 == high && 0xA4 == low) // ("长" 0xB3A4) 一般都 Chang pinyin[0] = 'C'; strPY = [strPY stringByAppendingFormat:@"%s", pinyin]; } break; } i++; } } // 特殊处理 { unsigned char test[8] = {0}; memset(test, 0 , sizeof(test)); if(strlen(pszHZ) >= 4) { memcpy(test, pszHZ, 4); if(0xD6 == test[0] && 0xD8 == test[1] && 0xC7 == test[2] && 0xEC == test[3]) { //0xD6D8重 0xC7EC庆 const char* ss = [strPY cStringUsingEncoding:NSASCIIStringEncoding]; NSString* strTemp = @""; strPY = [strTemp stringByAppendingFormat:@"C%s", ss+1]; } else if(0xCE == test[0] && 0xF7 == test[1] && 0xB2 == test[2] && 0xD8 == test[3]) { //0xCEF7西 0xB2D8藏 const char* ss = [strPY cStringUsingEncoding:NSASCIIStringEncoding]; NSString* strTemp = @""; strPY = [strTemp stringByAppendingFormat:@"XZ%s", ss+2]; } } else if(strlen(pszHZ) >= 8) { memcpy(test, pszHZ, 8); if(0xD5 == test[0] && 0xD0 == test[1] && 0xC9 == test[2] && 0xCC == test[3] && 0xB3 == test[4] && 0xC9 == test[5] && 0xB3 == test[6] && 0xA4 == test[7]) { //0xD5D0招 0xC9CC商 0xB3C9成 0xB3A4长 const char* ss = [strPY cStringUsingEncoding:NSASCIIStringEncoding]; if(4 == strlen(ss)) strPY = @"ZSCZ"; else if(strlen(ss) > 4) { NSString* strTemp = @""; strPY = [strTemp stringByAppendingFormat:@"ZSCZ%s", ss+4]; } } } } return strPY; }
上述代码所需数组变量下载 SymbolSearchController.txt.zip (30 K