代码:
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace ArithmeticDemo
{
public class GetPY
{
public static void Main()
{
GetPY ob1 = new GetPY();
Console.WriteLine(ob1.GetPYString("你好士大夫艾丝凡df"));
Console.ReadLine();
}
public string GetPYString(string str)
{
string tempStr = "";
foreach (char c in str)
{
if ((int)c >= 33 && (int)c <= 126)
{
tempStr += c.ToString(); //字母和符号原样保留
}
else
{
tempStr += GetPYChar(c.ToString());//累加拼音声母
}
}
return tempStr;
}
public string GetPYChar(string c)
{
byte[] array = new byte[2];
array = System.Text.Encoding.Default.GetBytes(c);
int i = (short)(array[0] - '\0') * 256 + ((short)(array[1] - '\0'));
if (i < 0xB0A1) return "*";
if (i < 0xB0C5) return "a";
if (i < 0xB2C1) return "b";
if (i < 0xB4EE) return "c";
if (i < 0xB6EA) return "d";
if (i < 0xB7A2) return "e";
if (i < 0xB8C1) return "f";
if (i < 0xB9FE) return "g";
if (i < 0xBBF7) return "h";
if (i < 0xBFA6) return "g";
if (i < 0xC0AC) return "k";
if (i < 0xC2E8) return "l";
if (i < 0xC4C3) return "m";
if (i < 0xC5B6) return "n";
if (i < 0xC5BE) return "o";
if (i < 0xC6DA) return "p";
if (i < 0xC8BB) return "q";
if (i < 0xC8F6) return "r";
if (i < 0xCBFA) return "s";
if (i < 0xCDDA) return "t";
if (i < 0xCEF4) return "w";
if (i < 0xD1B9) return "x";
if (i < 0xD4D1) return "y";
if (i < 0xD7FA) return "z";
return "*";
}
}
}
GB2312编码规则:
GB2312标准共收录6763个汉字,其中一级汉字3755个,二级汉字3008个;同时,GB2312收录了包括拉丁字母、希腊字母、日文平假名及片假名字母、俄罗斯语西里尔字母在内的682个全形字符。
GB2312的出现,基本满足了汉字的计算机处理需要,它所收录的汉字已经覆盖99.75%的使用频率。GB2312中对所收汉字进行了“分区”处理,每区含有94个汉字/符号。这种表示方式也称为区位码。
01-09区为特殊符号。
16-55区为一级汉字,按拼音排序。
56-87区为二级汉字,按部首/笔画排序。
10-15区及88-94区则未有编码。
举例来说,“啊”字是GB2312之中的第一个汉字,它的区位码就是1601。字节结构在使用GB2312的程序中,通常采用EUC储存方法,以便兼容于ASCII。每个汉字及符号以两个字节来表示。第一个字节称为“高位字节”,第二个字节称为“低位字节”。 “高位字节”使用了0xA1-0xF7(把01-87区的区号加上0xA0),“低位字节”使用了0xA1-0xFE(把01-94加上0xA0)。例如“啊”字在大多数程序中,会以0xB0A1储存。(与区位码对比:0xB0=0xA0+16,0xA1=0xA0+1)。
所以GB2312编码中汉字区码的十进制是从176到247,位码是从161到255.之所以存储了6763小于82*94=6768,是因为在区码为215,位码为250-254之间共五个编码没有汉字编码,所以6768-5=6763个。
其他链接:http://herongyang.com/gb2312_gb/pinyin.html
http://www.knowsky.com/resource/gb2312tbl.htm