2012-10-26 4 views
1

WebClient를 통해 서명 된 XML을 게이트웨이로 보냅니다. 이제 노드 값에는 독일어 문자 만 포함되어야합니다. 나는 2 개의 Testwords가있다.Encoding.Convert로 특정 문자를 제거하는 방법

string foreignString = "Łůj꣥ü"; 
Encoding utf8 = Encoding.UTF8; 
Encoding iso = Encoding.GetEncoding("ISO-8859-1"); 
byte[] utfBytes = Encoding.Convert(iso, utf8, iso.GetBytes(foreignString)); 
string result = utf8.GetString(utfBytes); 

을하지만 두 번째 문자열에 또한 UTF-8 인코딩에 포함 된 문자이다 : 첫 번째는 아주 잘 사용하여 변환됩니다. 그것의 the

ç (Latin small letter c with cedilla) 

다른 인코딩으로 조금 테스트 한 후에 나는 항상 같은 결과를 얻었습니다. 캐릭터는 항상 거기에있었습니다. UTF-8 테이블의 일부이기 때문에 어떤 의미가 있습니까 :)

제 질문은 : 독일어 움라우트를 삭제하지 않고 모든 프랑스어, 포르투갈어 및 스페인어 문자를 숨길 수있는 방법이 있습니까?

미리 감사드립니다.

답변

2
당신은 당신의 추가 특별 규정과 ISO-8859-1 인코딩을 기반으로 자신의 Encoding 클래스를 만들 수 있습니다

:이 인코딩은 당신이 ISO-8859-를 사용하려면 사실에 근거

class GermanEncoding : Encoding { 

    static readonly Encoding iso88791Encoding = Encoding.GetEncoding("ISO-8859-1"); 

    static readonly Dictionary<Char, Char> charMappingTable = new Dictionary<Char, Char> { 
    { 'À', 'A' }, 
    { 'Á', 'A' }, 
    { 'Â', 'A' }, 
    { 'ç', 'c' }, 
    // Add more mappings 
    }; 

    static readonly Dictionary<Byte, Byte> byteMappingTable = charMappingTable 
    .ToDictionary(kvp => MapCharToByte(kvp.Key), kvp => MapCharToByte(kvp.Value)); 

    public override Int32 GetByteCount(Char[] chars, Int32 index, Int32 count) { 
    return iso88791Encoding.GetByteCount(chars, index, count); 
    } 

    public override Int32 GetBytes(Char[] chars, Int32 charIndex, Int32 charCount, Byte[] bytes, Int32 byteIndex) { 
    var count = iso88791Encoding.GetBytes(chars, charIndex, charCount, bytes, byteIndex); 
    for (var i = byteIndex; i < byteIndex + count; ++i) 
     if (byteMappingTable.ContainsKey(bytes[i])) 
     bytes[i] = byteMappingTable[bytes[i]]; 
    return count; 
    } 

    public override Int32 GetCharCount(Byte[] bytes, Int32 index, Int32 count) { 
    return iso88791Encoding.GetCharCount(bytes, index, count); 
    } 

    public override Int32 GetChars(Byte[] bytes, Int32 byteIndex, Int32 byteCount, Char[] chars, Int32 charIndex) { 
    return iso88791Encoding.GetChars(bytes, byteIndex, byteCount, chars, charIndex); 
    } 

    public override Int32 GetMaxByteCount(Int32 charCount) { 
    return iso88791Encoding.GetMaxByteCount(charCount); 
    } 

    public override Int32 GetMaxCharCount(Int32 byteCount) { 
    return iso88791Encoding.GetMaxCharCount(byteCount); 
    } 

    static Byte MapCharToByte(Char c) { 
    // NOTE: Assumes that each character encodes as a single byte. 
    return iso88791Encoding.GetBytes(new[] { c })[0]; 
    } 

} 

1 인코딩과 "ASCII 문자가 아닌"문자를 매핑하려는 몇 가지 추가 제한이 있습니다. 내장 ISO-8859-1 인코딩은 ŁL에 매핑하는 방법을 알고 있으며 ISO-8859-1이 단일 바이트 문자 세트이기 때문에 각 바이트가 문자에 해당하기 때문에 바이트에 추가 매핑을 수행 할 수 있습니다. 이는 GetBytes 메소드에서 수행됩니다.

이 코드를 사용하여 문자열을 "청소"할 수

var encoding = new GermanEncoding(); 
string foreignString = "Łůj꣥üç"; 
var bytes = encoding.GetBytes(foreignString); 
var result = encoding.GetString(bytes); 

결과 문자열은 LujeLAüc입니다.

구현은 매우 단순하며 사전을 사용하여 바이트의 추가 매핑 단계를 수행합니다. 이것은 효율적이지 않을 수도 있지만이 경우 256 바이트 매핑 배열을 사용하는 것과 같은 대안을 고려할 수 있습니다. 또한 수행 할 추가 매핑이 모두 포함되도록 charMappingTable을 확장해야합니다.

+0

멋진 솔루션을 제공해 주셔서 감사합니다. – csteinmueller

관련 문제