[C#] Из CP1251 -> UTF-8

Discussion in 'С/С++, C#, Rust, Swift, Go, Java, Perl, Ruby' started by bafoed, 27 Jan 2011.

  1. bafoed

    bafoed Member

    Joined:
    14 Jul 2009
    Messages:
    88
    Likes Received:
    12
    Reputations:
    10
    принимаю запрос с акапчи, русская отображается вот так:
    [​IMG]

    При перекодировке CP1251 → UTF-8 получаю текст в нормальном виде. как перекодировать на C#?
     
  2. cheater_man

    cheater_man Member

    Joined:
    13 Nov 2009
    Messages:
    651
    Likes Received:
    44
    Reputations:
    7
    #2 cheater_man, 27 Jan 2011
    Last edited: 27 Jan 2011
  3. bafoed

    bafoed Member

    Joined:
    14 Jul 2009
    Messages:
    88
    Likes Received:
    12
    Reputations:
    10
    Code:
    private string Win1251ToUTF8(string source)
    {
    
    Encoding utf8 = Encoding.GetEncoding("utf-8");
    Encoding win1251 = Encoding.GetEncoding("windows-1251");
    
    byte[] utf8Bytes = win1251.GetBytes(source);
    byte[] win1251Bytes = Encoding.Convert(win1251, utf8, utf8Bytes);
    source = win1251.GetString(win1251Bytes);
    return source;
    
    }
    
    [​IMG]
     
  4. cheater_man

    cheater_man Member

    Joined:
    13 Nov 2009
    Messages:
    651
    Likes Received:
    44
    Reputations:
    7
    далеко пойдешь=)
    Code:
    private string Win1251ToUTF8(string source)
            {
    
            Encoding utf8 = Encoding.GetEncoding("UTF-8");
            Encoding win1251 = Encoding.GetEncoding("Windows-1251");
    
            byte[] utf8Bytes = win1251.GetBytes(source);
            byte[] win1251Bytes = Encoding.Convert(utf8, win1251, utf8Bytes);
            source = win1251.GetString(win1251Bytes);
            return source;
    
            }