漢字轉拼音源碼
『壹』 在asp中怎樣通過中文名字的首字母來查詢姓名列表
比較簡單易實現的就是你自己直接添加一個欄位為明星的首字母,這樣通過查詢這個欄位就可以實現這個功能了,其他的都是很復雜龐大的工程。。。
其他參考代碼
原理,使用Dictionary技術
1.添加索引
2.遍歷詞典
<%
Set d = CreateObject("Scripting.Dictionary")
d.add "a",-20319
d.add "ai",-20317
d.add "an",-20304
d.add "ang",-20295
d.add "ao",-20292
d.add "ba",-20283
d.add "",-20265
d.add "ban",-20257
d.add "bang",-20242
d.add "bao",-20230
d.add "bei",-20051
d.add "ben",-20036
d.add "beng",-20032
d.add "bi",-20026
d.add "bian",-20002
d.add "biao",-19990
d.add "bie",-19986
d.add "bin",-19982
d.add "bing",-19976
d.add "bo",-19805
d.add "bu",-19784
d.add "ca",-19775
d.add "cai",-19774
d.add "can",-19763
d.add "cang",-19756
d.add "cao",-19751
d.add "ce",-19746
d.add "ceng",-19741
d.add "cha",-19739
d.add "chai",-19728
d.add "chan",-19725
d.add "chang",-19715
d.add "chao",-19540
d.add "che",-19531
d.add "chen",-19525
d.add "cheng",-19515
d.add "chi",-19500
d.add "chong",-19484
d.add "chou",-19479
『貳』 java如何獲取漢字的拼音字母
獲取首字母需要對漢字表和字母表進行映射,如下示例代碼是以gb2312編碼為入手點,進行匹配的,也可以使用gbk、utf-8等編碼進行匹配,但代碼就完全不同了。
示例代碼如下:
public class FirstLetterUtils {
// 簡體中文的編碼范圍從B0A1(45217)一直到F7FE(63486)
private static int BEGIN = 45217;
private static int END = 63486;
// 按照聲 母表示,這個表是在GB2312中的出現的第一個漢字,也就是說「啊」是代表首字母a的第一個漢字。
// i, u, v都不做聲母, 自定規則跟隨前面的字母
private static char[] chartable = { '啊', '芭', '擦', '搭', '蛾', '發', '噶', '哈', '哈', '擊', '喀', '垃', '媽', '拿', '哦', '啪', '期', '然', '撒', '塌', '塌', '塌', '挖', '昔', '壓', '匝', };
// 二十六個字母區間對應二十七個端點
// GB2312碼漢字區間十進製表示
private static int[] table = new int[27];
// 對應首字母區間表
private static char[] initialtable = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'h', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 't', 't', 'w', 'x', 'y', 'z', };
// 初始化
static {
for (int i = 0; i < 26; i++) {
table[i] = gbValue(chartable[i]);// 得到GB2312碼的首字母區間端點表,十進制。
}
table[26] = END;// 區間表結尾
}
// ------------------------public方法區------------------------
// 根據一個包含漢字的字元串返回一個漢字拼音首字母的字元串 最重要的一個方法,思路如下:一個個字元讀入、判斷、輸出
public static String cn2py(String SourceStr) {
String Result = "";
int StrLength = SourceStr.length();
int i;
try {
for (i = 0; i < StrLength; i++) {
Result += Char2Initial(SourceStr.charAt(i));
}
} catch (Exception e) {
Result = "";
e.printStackTrace();
}
return Result;
}
// ------------------------private方法區------------------------
/**
* 輸入字元,得到他的聲母,英文字母返回對應的大寫字母,其他非簡體漢字返回 '0' *
*/
private static char Char2Initial(char ch) {
// 對英文字母的處理:小寫字母轉換為大寫,大寫的直接返回
if (ch >= 'a' && ch <= 'z') {
return (char) (ch - 'a' + 'A');
}
if (ch >= 'A' && ch <= 'Z') {
return ch;
}
// 對非英文字母的處理:轉化為首字母,然後判斷是否在碼表范圍內,
// 若不是,則直接返回。
// 若是,則在碼表內的進行判斷。
int gb = gbValue(ch);// 漢字轉換首字母
if ((gb < BEGIN) || (gb > END))// 在碼表區間之前,直接返回
{
return ch;
}
int i;
for (i = 0; i < 26; i++) {// 判斷匹配碼表區間,匹配到就break,判斷區間形如「[,)」
if ((gb >= table[i]) && (gb < table[i + 1])) {
break;
}
}
if (gb == END) {// 補上GB2312區間最右端
i = 25;
}
return initialtable[i]; // 在碼表區間中,返回首字母
}
/**
* 取出漢字的編碼 cn 漢字
*/
private static int gbValue(char ch) {// 將一個漢字(GB2312)轉換為十進製表示。
String str = new String();
str += ch;
try {
byte[] bytes = str.getBytes("GB2312");
if (bytes.length < 2) {
return 0;
}
return (bytes[0] << 8 & 0xff00) + (bytes[1] & 0xff);
} catch (Exception e) {
return 0;
}
}
public static void main(String[] args) throws Exception {
System.out.println(cn2py("這是一個獲取首字母的class"));
}
}
『叄』 如何用C++實現漢字轉拼音系統 相當急啊!
如果是做課設什麼告鍵帆的,只需要簡單的示例,只需要輸入幾個特定的漢字的話。
可以搜索一下「中英翻譯機 C語言」就可以找到,把英文換成漢字,翻譯變成拼音就行了,很簡單的。
C++嘛,就是把printf換成cout,又不規定一定要用類來做。
如果要神馬漢亮螞字都能轉,貌似需要用到什麼字型檔。
搜索一下襪雹同樣能找到源代碼。