Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

Konvertovanje iz ćirilice u latinicu

[es] :: Pascal / Delphi / Kylix :: Konvertovanje iz ćirilice u latinicu

[ Pregleda: 4216 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

zm5056
Pavle Pavić
BG

Član broj: 65892
Poruke: 23
*.static.isp.telekom.rs.



Profil

icon Konvertovanje iz ćirilice u latinicu28.03.2012. u 13:46 - pre 146 meseci
Pokušavao sam tekst iz editbox-a da konvertujem iz ćirilice u latinicu.

Code:

Function CirToLatin(tekst:WideString):WideString;
var
i:integer;
begin
        for i := 1 to Length(tekst) do
        begin

           case tekst[i] of
           'B':tekst[i]:='B';
           'V':tekst[i]:='V';
           'G':tekst[i]:='G';
           'D':tekst[i]:='D';
           'Đ':tekst[i]:='Đ';
           'Ž':tekst[i]:='Ž';
           'Z':tekst[i]:='Z';
           'I':tekst[i]:='I';
           'L':tekst[i]:='L';
           'Lj':tekst[i]:='L';
           'N':tekst[i]:='N';
           'Nj':tekst[i]:='N';
           'P':tekst[i]:='P';
           'R':tekst[i]:='R';
           'S':tekst[i]:='S';
           'Ć':tekst[i]:='Ć';
           'U':tekst[i]:='U';
           'F':tekst[i]:='F';
           'H':tekst[i]:='H';
           'C':tekst[i]:='C';
           'Č':tekst[i]:='Č';
           'Dž':tekst[i]:='D';
           'Š':tekst[i]:='Š';
           end;
       end;
        end;
     Result:=tekst;
end;

Ovaj moj pokušaj ne radi za slova Č,Ć,Ž,Š, ispisuje mi kukiceŽ.
Kako da konvertujem Nj,Lj i Dž?
 
Odgovor na temu

savkic
Igor Savkić

Moderator
Član broj: 92186
Poruke: 2739



+92 Profil

icon Re: Konvertovanje iz ćirilice u latinicu28.03.2012. u 14:57 - pre 146 meseci
> Ovaj moj pokušaj ne radi za slova Č,Ć,Ž,Š, ispisuje mi kukiceŽ.

Možda nemaš dobar (unicode) font, takođe moraš imati unicode kontrolu.

> Kako da konvertujem Nj,Lj i Dž?

Moraš da jedno slovo zameniš sa dva.

Šaljem primer funkcija za konverziju iz latinice u ćirilicu i obratno, rađene su za AnsiString ali verujem da ti mogu koristiti i za Unicode.

Code:


var
  MapWin1250to1251: array of Char;
  MapWin1251to1250: array of Char;

procedure CreateCharConversionMap(const ACodePage: Smallint);
const
  Win1250_Capital = 'ABCDEFGHIJKLMNOPQRSTUVWXYZÈÆŠÐŽ';
  Win1251_Capital = #192#193#214#196#197#212#195#213#200#163#202#203#204#205#206#207'Q'#208#209#210#211#194'WXY'#199#215#142#216#128#198;
  Win1250_Small   = 'abcdefghijklmnopqrstuvwxyzèæšðž';
  Win1251_Small   = #224#225#246#228#229#244#227#245#232#188#234#235#236#237#238#239'q'#240#241#242#243#226'wxy'#231#247#158#248#144#230;
  Win1251_ThreeLetters = #140#156#138#154#143#159; // Nj, nj, Lj, lj, Dž, dž
  Win1250_ThreeLetters = 'NnLlDd';
var
  i: Integer;
  Source, Dest: string;
begin
  case ACodePage of
    1250:
    begin
      if Length(MapWin1250to1251) = 0 then
      begin
        Source := Win1250_Capital + Win1250_Small;
        Dest := Win1251_Capital + Win1251_Small;
        SetLength(MapWin1250to1251, 256);

        for i := 1 to Length(Source) do
          MapWin1250to1251[Ord(Source[i])] := Dest[i];
      end;
    end;

    1251:
    begin
      if Length(MapWin1251to1250) = 0 then
      begin
        Source := Win1251_Capital + Win1251_Small + Win1251_ThreeLetters;
        Dest := Win1250_Capital + Win1250_Small + Win1250_ThreeLetters;
        SetLength(MapWin1251to1250, 256);

        for i := 1 to Length(Source) do
          MapWin1251to1250[Ord(Source[i])] := Dest[i];
      end;
    end;
  end;
end;

function Win1250to1251(const AValue: string): string;
var
  Source, Dest: PChar;
begin
  // Konvertujemo latinicu po kodnoj strani win1250 u cirilicu win1251
  CreateCharConversionMap(1250);

  SetLength(Result, Length(AValue));

  Source := PChar(AValue);
  Dest := PChar(Result);
  while Source^ <> #0 do
  begin
    if MapWin1250to1251[Ord(Source^)] <> #0 then
    begin
      Dest^ := MapWin1250to1251[Ord(Source^)];
      case Source^ of
        'N', 'n', 'L', 'l':
        begin
          if (Source + 1)^ in ['J', 'j'] then
          begin
            case Source^ of
              'N': Dest^ := #140;
              'n': Dest^ := #156;
              'L': Dest^ := #138;
              'l': Dest^ := #154;
            end;

            Inc(Source);
          end;
        end;

        'D', 'd':
        begin
          if (Source + 1)^ in ['Ž', 'ž'] then
          begin
            if Source^ = 'D' then
              Dest^ := #143
            else
              Dest^ := #159;

            Inc(Source);
          end;
        end;
      end;
    end
    else
      Dest^ := Source^;

    Inc(Dest);
    Inc(Source);
  end;

  SetLength(Result, Dest - PChar(Result));
end;

function Win1251to1250(const AValue: string): string;
var
  Source, Dest: PChar;
  Temp: Integer;
begin
  // Konvertujemo cirilicu u kodnoj strani Win1251 u latinicu Win1250
  CreateCharConversionMap(1251);

  SetLength(Result, Length(AValue));

  Source := PChar(AValue);
  Dest := PChar(Result);
  while Source^ <> #0 do
  begin
    if MapWin1251to1250[Ord(Source^)] <> #0 then
    begin
      Dest^ := MapWin1251to1250[Ord(Source^)];
      if Source^ in [#140, #156, #138, #154, #143, #159] then
      begin
        Temp := Dest - PChar(Result);
        SetLength(Result, Length(Result) + 1);
        Dest := PChar(Result) + Temp + 1;

        if Source^ in [#143, #159] then
          Dest^ := 'ž'
        else
          Dest^ := 'j';
      end;
    end
    else
      Dest^ := Source^;

    Inc(Dest);
    Inc(Source);
  end;
end;

 
Odgovor na temu

[es] :: Pascal / Delphi / Kylix :: Konvertovanje iz ćirilice u latinicu

[ Pregleda: 4216 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.