void LoadOneTable( XmlNode root, ref CodePage page) { Debug.Assert(page != null, ""); // <characterSet name="Basic Arabic" ISOcode="33"> XmlNodeList nodes = root.SelectNodes("code"); /* * <code> * <marc>21</marc> * <ucs>0021</ucs> * <utf-8>21</utf-8> * <name>EXCLAMATION MARK</name> * </code> * */ for (int i = 0; i < nodes.Count; i++) { XmlNode node = nodes[i]; CodeEntry entry = new CodeEntry(); string strASCII = DomUtil.GetElementText(node, "marc"); string strUnicode = DomUtil.GetElementText(node, "ucs"); if (String.IsNullOrEmpty(strASCII) == true || String.IsNullOrEmpty(strUnicode) == true) { continue; } entry.ASCII = System.Convert.ToInt32(strASCII, 16); entry.Unicode = System.Convert.ToInt32(strUnicode, 16); page.Add(entry); } }
int AsciiToUnicode(int iCodeG0, int iCodeG1, string strSource, out string strTarget, out string strError) { strTarget = ""; strError = ""; CodePage pageG0 = null; CodePage pageG1 = null; pageG0 = FindPage(iCodeG0); if (pageG0 == null) { strError = "不能识别的G0代码页 0x" + System.Convert.ToString(iCodeG0, 16); return(-1); } pageG1 = FindPage(iCodeG1); if (pageG1 == null) { strError = "不能识别的G1代码页 0x" + System.Convert.ToString(iCodeG1, 16); return(-1); } for (int i = 0; i < strSource.Length; i++) { byte b = (byte)strSource[i]; int u = 0; if (b >= 0xa1 && b <= 0xfe) { // 应用G1代码页 // b -= 0x80; u = pageG1.GetUnicode(b); strTarget += (char)u; } else if (b >= 0x21 && b <= 0x7e) { // 应用G0代码页 u = pageG0.GetUnicode(b); strTarget += (char)u; } else { strTarget += strSource[i]; continue; } } return(0); }
CodePage FindPage(int iCode) { for (int i = 0; i < this.CodePages.Count; i++) { CodePage page = this.CodePages[i]; if (page.Code == iCode) { return(page); } } return(null); }
public void BuildASCIiTables(string strXmlFileName) { XmlDocument dom = new XmlDocument(); dom.Load(strXmlFileName); // <characterSet name="Basic Arabic" ISOcode="33"> XmlNodeList nodes = dom.DocumentElement.SelectNodes("//characterSet"); this.CodePages = new List <CodePage>(); for (int i = 0; i < nodes.Count; i++) { XmlNode node = nodes[i]; CodePage page = new CodePage(); page.Name = DomUtil.GetAttr(node, "name"); page.Code = System.Convert.ToInt32(DomUtil.GetAttr(node, "ISOcode"), 16); LoadOneTable(node, ref page); this.CodePages.Add(page); } }