2010-04-12 69 views
12

如何将不同的Unicode字符转换为最接近的ASCII字符?像Ä - > A.我使用Google搜索,但没有找到任何合适的解决方案。技巧Encoding.ASCII.GetBytes("Ä")[0]没有工作。 (结果是?)。将Unicode字符转换为ASCII(.NET)中最接近的(最相似的)字符

我发现,有一类Encoder具有Fallback属性,正是针对当char不能转换,但实现(EncoderReplacementFallback)是愚蠢的,并转换为?

任何想法?

+4

你是如何定义“最接近的ASCII等价物”?只需删除重音标记? – mmr 2010-04-12 19:12:51

+0

那么......如果有任何定义,除了转换为?我想它:)我认为在口音的情况下,它只是删除。 – Andrey 2010-04-12 19:15:20

+0

@mmr:我不知道安德烈的用法,但我有一个应该接受文本的程序,并将它提供给一个连接的设备,稍后将显示它。多种连接的设备无法承担存储多字节字符所需的RAM,并且无论如何,无法承受存储超过256个字符字形所需的ROM [大多数字符矩阵LCD模块具有固定的160个字符设置加显示八个同时自定义5x7或5x8字形的能力]。将所有非ASCII文本呈现为“?”似乎是不必要的丑陋。 – supercat 2014-01-15 19:24:25

回答

7

如果只是去除diacritical marks的,然后前往this answer

static string RemoveDiacritics(string stIn) { 
    string stFormD = stIn.Normalize(NormalizationForm.FormD); 
    StringBuilder sb = new StringBuilder(); 

    for(int ich = 0; ich < stFormD.Length; ich++) { 
    UnicodeCategory uc = CharUnicodeInfo.GetUnicodeCategory(stFormD[ich]); 
    if(uc != UnicodeCategory.NonSpacingMark) { 
     sb.Append(stFormD[ich]); 
    } 
    } 

    return(sb.ToString().Normalize(NormalizationForm.FormC)); 
} 
2

MS Dynamics有一个问题,它不允许x20到x7f以外的任何字符,并且该范围内的某些字符也是无效的。我的答案是创建一个键入无效字符的数组,以返回有效字符的最佳猜测。
它不漂亮,但它的工作原理。

Function PlainAscii(InText) 
Dim i, c, a 
Const cUTF7 = "^[\x20-\x7e]+$" 
Const IgnoreCase = False 
    PlainAscii = "" 
    If InText = "" Then Exit Function 
    If RegExTest(InText, cUTF7, IgnoreCase) Then 
     PlainAscii = InText 
    Else 
     For i = 1 To Len(InText) 
      c = Mid(InText, i, 1) 
      a = Asc(c) 
      If a = 10 Or a = 13 Or a = 9 Then 
       ' Do Nothing - Allow LF, CR & TAB 
      ElseIf a < 32 Then 
       c = " " 
      ElseIf a > 126 Then 
       c = CvtToAscii(a) 
      End If 
      PlainAscii = PlainAscii & c 
     Next 
    End If 
End Function 

Function CvtToAscii(inChar) 
' Maps The Characters With The 8th Bit Set To 7 Bit Characters 
Dim arrChars 
    arrChars = Array(" ", " ", "$", " ", ",", "f", """", " ", "t", "t", "^", "%", "S", "<", "O", " ", "Z", " ", " ", "'", "'", """", """", ".", "-", "-", "~", "T", "S", ">", "o", " ", "Z", "Y", " ", "!", "$", "$", "o", "$", "|", "S", " ", "c", " ", " ", " ", "_", "R", "_", ".", " ", " ", " ", " ", "u", "P", ".", ",", "i", " ", " ", " ", " ", " ", " ", "A", "A", "A", "A", "A", "A", "A", "C", "E", "E", "E", "E", "I", "I", "I", "I", "D", "N", "O", "O", "O", "O", "O", "X", "O", "U", "U", "U", "U", "Y", "b", "B", "a", "a", "a", "a", "a", "a", "a", "c", "e", "e", "e", "e", "i", "i", "i", "i", "o", "n", "o", "o", "o", "o", "o", "/", "O", "u", "u", "u", "u", "y", "p", "y") 
    CvtToAscii = arrChars(inChar - 127) 
End Function 

Function RegExTest(ByVal strStringToSearch, strExpression, IgnoreCase) 
Dim objRegEx 
    On Error Resume Next 
    Err.Clear 
    strStringToSearch = Replace(Replace(strStringToSearch, vbCr, ""), vbLf, "") 
    RegExTest = False 
    Set objRegEx = New RegExp 
    With objRegEx 
     .Pattern = strExpression '//the reg expression that should be searched for 
     If Err.Number = 0 Then 
      .IgnoreCase = CBool(IgnoreCase) '//not case sensitive 
      .Global = True    '//match all instances of pattern 
      RegExTest = .Test(strStringToSearch) 
     End If 
    End With 
    Set objRegEx = Nothing 
    On Error Goto 0 
End Function 

您的答案肯定会有所不同。

+0

我会看看,谢谢 – Andrey 2010-04-12 19:20:05

相关问题