Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1062225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:35:33+00:00 2026-05-16T18:35:33+00:00

I want to encode strings as Python do. Python code is this: def EncodeToUTF(inputstr):

  • 0

I want to encode strings as Python do.

Python code is this:

def EncodeToUTF(inputstr):
  uns = inputstr.decode('iso-8859-2')
  utfs = uns.encode('utf-8')
  return utfs

This is very simple.

But in Delphi I don’t understand, how to encode, to force first the good character set (no matter, which computer we have).

I tried this test code to see the convertion:

procedure TForm1.Button1Click(Sender: TObject);
var
    w : WideString;
    buf : array[0..2048] of WideChar;
    i : integer;
    lc : Cardinal;
begin
    lc := GetThreadLocale;
    Caption := IntToStr(lc);
    StringToWideChar(Edit1.Text, buf, SizeOF(buf));
    w := buf;
    lc := MakeLCID(
        MakeLangID( LANG_ENGLISH, SUBLANG_ENGLISH_US),
        0);
    Win32Check(SetThreadLocale(lc));
    Edit2.Text := WideCharToString(PWideChar(w));
    Caption := IntToStr(AnsiCompareText(Edit1.Text, Edit2.Text));
end;

The input is: “árvíztűrő tükörfúrógép”, the hungarian accent tester phrase.
The local lc is 1038 (hun), the new lc is 1033.

But this everytime makes 0 result (same strings), and the accents are same, I don’t lost ŐŰ which is not in english lang.

What I do wrong? How to I do same thing as Python do?

Thanks for every help, link, etc:
dd

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:35:34+00:00Added an answer on May 16, 2026 at 6:35 pm

    Windows uses codepage 28592 for ISO-8859-2. If you have a buffer containing ISO-8859-2 encoded bytes, then you have to decode the bytes to UTF-16 first, and then encode the result to UTF-8. Depending on which version of Delphi you are using, you can either:

    1) on pre-D2009, use MultiByteToWideChar() and WideCharToMultiByte():

    function EncodeToUTF(const inputstr: AnsiString): UTF8String;
    var
      ret: Integer;
      uns: WideString;
    begin
      Result := '';
      if inputstr = '' then Exit;
      ret := MultiByteToWideChar(28592, 0, PAnsiChar(inputstr), Length(inputstr), nil, 0);
      if ret < 1 then Exit;
      SetLength(uns, ret);
      MultiByteToWideChar(28592, 0, PAnsiChar(inputstr), Length(inputstr), PWideChar(uns), Length(uns));
      ret := WideCharToMultiByte(65001, 0, PWideChar(uns), Length(uns), nil, 0, nil, nil);
      if ret < 1 then Exit;
      SetLength(Result, ret);
      WideCharToMultiByte(65001, 0, PWideChar(uns), Length(uns), PAnsiChar(Result), Length(Result), nil, nil);
    end;
    

    2a) on D2009+, use SysUtils.TEncoding.Convert():

    function EncodeToUTF(const inputstr: RawByteString): UTF8String;
    var
      enc: TEncoding;
      buf: TBytes;
    begin
      Result := '';
      if inputstr = '' then Exit;
      enc := TEncoding.GetEncoding(28592);
      try
        buf := TEncoding.Convert(enc, TEncoding.UTF8, BytesOf(inputstr));
        if Length(buf) > 0 then
          SetString(Result, PAnsiChar(@buf[0]), Length(buf));
      finally
        enc.Free;
      end;
    end;
    

    2b) on D2009+, alternatively define a new string typedef, put your data into it, and assign it to a UTF8String variable. No manual encoding/decoding needed, the RTL will handle everything for you:

    type
      Latin2String = type AnsiString(28592);
    
    var
      inputstr: Latin2String;
      outputstr: UTF8String;
    begin
      // put the ISO-8859-2 encoded bytes into inputstr, then...
      outputstr := inputstr;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to convert Python string to URL syntax. For example >>> u'한글'.encode('utf-8') '\xed\x95\x9c\xea\xb8\x80'
I want to convert unicode string into iso-8859-15. These strings include the u\u2019 (RIGHT
I want to encode and decode Bitmap object in string base64 . I use
I want to make python ignore chars it can't encode, by simply replacing them
This is a section from Dive Into Python 3 regarding strings: In Python 3,
I want to encode strings in to the Chinese and Korean languages how it
I'm a beginner to Python trying to decode this javascript sequence. I'm not only
I have string that displays UTF-8 encoded characters, and I want to convert it
I want to encode DNS protocol header using C and create a UDP datagram.
I want to encode binary file into base64 and put it into php page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.