2009-08-07 60 views
2

所以我有一个ASP.Net(vb.net)应用程序。它有一个文本框,用户正在将来自Microsoft Word的文本粘贴到其中。因此,诸如long dash(charcode 150)之类的东西即将通过输入。其他例子是聪明的引号或重音字符。在我的应用程序中,我将它们编码为xml,并将其作为XML存储过程的xml参数传递给数据库。它被插入到数据库中,就像用户输入它一样。字符支持问题 - 如何将较高的ASCII字符转换为较低的ASCII字符

问题是读取此数据的应用程序不喜欢这些字符。所以我需要将它们翻译成较低的ascii(我认为是7bit)字符集。我怎么做?我如何确定它们在哪些编码中,以便我可以执行以下操作。并且只是要求ASCII等价物能够智能地翻译它们,还是必须为此编写一些代码?

也可能在网页中解决这个问题可能更容易一开始。当您从Word中复制字符选择时,会在剪贴板中放入多种格式。直接的文字是我想要的。有没有办法让HTML文本框在用户粘贴到文本时获取该文本?我必须以某种方式设置网页的编码吗?

System.Text.Encoding.ASCII.GetString(System.Text.Encoding.GetEncoding(1251).GetBytes(text)) 

与输入编码成XML应用程序代码:

Protected Function RequestStringItem(_ 
     ByVal strName As System.String) As System.String 

     Dim strValue As System.String 

     strValue = Me.Request.Item(strName) 
     If Not (strValue Is Nothing) Then 
     RequestStringItem = strValue.Trim() 
     Else 
     RequestStringItem = "" 
     End If 

    End Function 

    ' I get the input from the textboxes into an array like this 
    m_arrInsertDesc(intIndex) = RequestStringItem("txtInsertDesc" & strValue) 
    m_arrInsertFolder(intIndex) = RequestInt32Item("cboInsertFolder" & strValue) 

    ' create xml file for inserts 
    strmInsertList = New System.IO.MemoryStream() 
    wrtInsertList = New System.Xml.XmlTextWriter(strmInsertList, System.Text.Encoding.Unicode) 

    ' start document and add root element 
    wrtInsertList.WriteStartDocument() 
    wrtInsertList.WriteStartElement("Root") 

    ' cycle through inserts 
    For intIndex = 0 To m_intInsertCount - 1 

    ' if there is an insert description 
    If m_arrInsertDesc(intIndex).Length > 0 Then 

     ' if the insert description is of the appropriate length 
     If m_arrInsertDesc(intIndex).Length <= 96 Then 

      ' add element to xml 
      wrtInsertList.WriteStartElement("Insert") 
      wrtInsertList.WriteAttributeString("insertdesc", m_arrInsertDesc(intIndex)) 
      wrtInsertList.WriteAttributeString("insertfolder", m_arrInsertFolder(intIndex).ToString()) 
      wrtInsertList.WriteEndElement() 

     ' if insert description is too long 
     Else 

      m_strError = "ERROR: INSERT DESCRIPTION TOO LONG" 
      Exit Function 

     End If 

    End If 

    Next 

    ' close root element and document 
    wrtInsertList.WriteEndElement() 
    wrtInsertList.WriteEndDocument() 
    wrtInsertList.Close() 

    ' when I add the xml as a parameter to the stored procedure I do this 
    cmdAddRequest.Parameters.Add("@insert_list", OdbcType.NText).Value = System.Text.Encoding.Unicode.GetString(strmInsertList.ToArray()) 
+0

这就是我输入的内容。 这是一些 - 带有“有趣”字符的文字,例如: 这就是我想要的输出。 这是一些 - 带有“有趣”字符的文字,例如:áíóññѺª¿?ÇüéâääååçêëèïîìÄÅÉæÆòòûùÿûÜÜ£¥?ƒá – 2009-08-07 16:08:26

回答

1

这似乎适用于短冲刺和简短的引号到正规报价。因为我的html页面具有以下内容类型。但它会将所有重音字符转换为问号。这不是剪贴板的文本版本所具有的。所以我更接近,我只是觉得我的目标编码错了。

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> 

System.Text.Encoding.ASCII.GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(m_arrFolderDesc(intIndex))) 

编辑:发现我的目的,正确的目标编码,这是1252

System.Text.Encoding.GetEncoding(1252).GetString(System.Text.Encoding.GetEncoding("iso-8859-1").GetBytes(m_arrFolderDesc(intIndex))) 
1

多大的这些输入的字符的范围是多少? 256? (每个字符适合一个字节)。如果这是真的,那么实现256值查找表并不困难。多年以来,我一直没有玩BASIC,但基本上你会DIM 256字节的数组,并用翻译的值填充数组,即'第'字节会得到'a'(因为它可以),但是第150个字节会得到连字符。

1

如果转换到非Unicode字符集,你会的过程中失去一些字符。如果读取数据的传统应用程序不需要进行任何字符串转换,则可能需要考虑使用UTF-7,并在它回到unicode世界时将其转换回来 - 这将保留所有特殊字符。