2009-08-07 70 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世界時將其轉換回來 - 這將保留所有特殊字符。