Ansi to unicode in Delphi 7?

Hi everyone!

I'm struggling with the following issue:

I put a unicode HTML document (UTF8 encoding) into a normal delphi7 String.
(Then I use this as an e-mail's body in Outlook, but that's off-topic)

I'd like to take this String, and replace a small part of it with text that is currently written using ISO-8859-2 encoded ANSI characters.

Of course this results in the ANSI characters turning into some garbage when viewed in Outlook or an HTML browser.

Is there a free third party library for Delphi7 that will let me convert ANSI to UTF8 then copy it back to my String?

Thanks in advance!
0
Steven
3/12/2010 8:44:42 PM
embarcadero.delphi.non-tech 5933 articles. 1 followers. Follow

9 Replies
10715 Views

Similar Articles

[PageSpeed] 42
Get it on Google Play
Get it on Apple App Store

> Is there a free third party library for Delphi7 that will let me
> convert ANSI to UTF8 then copy it back to my String?

The D7 RTL... Check out the AnsiToUTF8/Utf8ToAnsi functions.
0
Chris
3/12/2010 9:20:03 PM
On 12.03.2010 21:44, Steven Smith wrote:

> Is there a free third party library for Delphi7 that will let me convert ANSI to UTF8 then copy it back to my String?

Utf8Encode / Utf8Decode are functions in the Delphi RTL, included in 
Delphi 6 (or even before) and higher

Cheers,
-- 
Michael Justin
SCJP, SCJA
betasoft - Software for Delphi™ and for the Java™ platform
http://www.mikejustin.com - http://www.betabeans.de
0
Michael
3/13/2010 6:08:18 AM
<Steven Smith> wrote in message news:223069@forums.codegear.com...

> I'd like to take this String, and replace a small part of it with
> text that is currently written using ISO-8859-2 encoded
> ANSI characters.

You can't mix multiple encodings in a single HTML document.  You will have 
to convert the ISO characters to UTF-8 to preserve the document's original 
encoding.

> Is there a free third party library for Delphi7 that will let
> me convert ANSI to UTF8 then copy it back to my String?

You do not need a library.  Call the Win32 API MultiByteToWideChar() 
function using codepage 28592 to produce a UTF-16 encoded string.  You can 
then use the Win32 API WideCharToMultiByte() function using codepage 65001 
(or the VCL UTF8Encode() function) to produce a UTF-8 encoded string.  For 
example:

{code:delphi}
function Latin2ToUtf8(const S: AnsiString): UTF8String;
var
  W: WideString;
  Len: Integer;
begin
  SetLength(W, MultiByteToWideChar(28592, 0, PAnsiChar(S), Length(S), nil, 
0));
  MultiByteToWideChar(28592, 0, PAnsiChar(S), Length(S), PWideChar(W), 
Length(W));
  SetLength(Result, WideCharToMultiByte(65001, 0, PWideChar(W), Length(W), 
nil, 0, nil, nil));
  WideCharToMultiByte(65001, 0, PWideChar(W), Length(W), PAnsiChar(Result), 
Length(Result), nil, nil);
end;
{code}

If you ever upgrade to D2009+, AnsiString's new codepage support will 
greatly simplify the code:


{code:delphi}
type
  Latin2String = type AnsiString(28592);
var
  Src: Latin2String;
  Dest: UTF8String;
begin
  Src := '...';
  Dest := Src;
{code}

-- 
Remy Lebeau (TeamB)
0
Remy
3/13/2010 9:49:49 AM
"Chris Rolliston" <spam@gmail.com> wrote in message 
news:223092@forums.codegear.com...

> The D7 RTL... Check out the AnsiToUTF8/Utf8ToAnsi functions.

Those functions use the OS default Ansi codepage for their conversions.  If 
your OS does not match the encoding of the strings you are working with, 
then they will not encode/decode correctly.

-- 
Remy Lebeau (TeamB)
0
Remy
3/13/2010 10:03:19 AM
> > The D7 RTL... Check out the AnsiToUTF8/Utf8ToAnsi functions.
> 
> Those functions use the OS default Ansi codepage for their
> conversions.  If your OS does not match the encoding of the strings
> you are working with

Where did the OP say they might not? Anyhow, in such a case, you can
either use the API directly (which obviously isn't hard) or call
SetMultiByteConversionCodePage before the actual conversion functions.
0
Chris
3/13/2010 12:11:27 PM
Thanks for the suggestions from everyone,

I didn't have time to test it yet, but...

How do I append an UTF8String to a String? Will a MyString:=MyString + String(UTF8StringWithSomeStuff) typecast work?

Outlook wants me to set the HTMLBody property (through COM automation) as a String/WideString.

Thanks!
0
Steven
3/13/2010 5:49:30 PM
<Steven Smith> wrote in message news:223323@forums.codegear.com...

> How do I append an UTF8String to a String?

Treat it like any other string.  Prior to D2009, UTF8String was just an 
alias for the standard AnsiString type:

{code:delphi}
type
  UTF8String = AnsiString;
{code}

In D2009 and later, it is its own unique string type now:

{code:delphi}
type
  UTF8String = type AnsiString(65001);
{code}

> Will a MyString:=MyString + String(UTF8StringWithSomeStuff) typecast work?

Yes.  Keep in mind that moving forward, String is Unicode in D2009+, so 
casting a UTF8String to a String will decode the UTF-8 data to UTF-16.  No 
such decode occurs in pre-D2009, where String is still Ansi and UTF8String 
is a plain AnsiString.

> Outlook wants me to set the HTMLBody property
> (through COM automation) as a String/WideString.

You will have to decode the UTF-8 data to UTF-16 first, as that is what 
WideString uses.  If you use a plain String typecast, the UTF-8 will not be 
decoded in pre-2009 versions, so do it manually, ie:

{code:delphi}
var
  W: WideString;
begin
  W := WideString(AnsiStringWithSomeStuff) + 
UTF8Decode(UTF8StringWithSomeStuff);
end;
{code}

In D2009:+


{code:delphi}
var
  W: WideString;
begin
  W := WideString(StringWithSomeStuff + String(UTF8StringWithSomeStuff));
end;
{code}

-- 
Remy Lebeau (TeamB)
0
Remy
3/14/2010 3:51:32 AM
Steven Smith schrieb:

> Is there a free third party library for Delphi7 that will let me
> convert ANSI to UTF8 then copy it back to my String?

You can use MultiByteToWideChar etc. WinAPI functions. Please note that 
two conversions are required: Ansi -> WideString -> UTF8.

For use with external applications, that accept wide strings, you may 
stay with the WideString, and omit the final UTF-8 conversion.

DoDi
0
Hans
3/14/2010 5:38:06 AM
Thanks for the answers guys!

Actually, I'm writing a program which lets the user store e-mails (form Outlook) and reply to them (using Outlook).

The e-mails are either stored as plain text or html.

Plain text e-mails are usually ansi encoded.

Html e-mails are either ansi or utf-8.

So I've decided to go with this (not so elegant) solution:

if Data.IsHTML and (LeftStr(Data.Body, 3) = '') then
        ReplyTemplate:=StringReplace(ReplyTemplate, '%Subject%', Utf8Encode(Data.Subject), [])
    else
        ReplyTemplate:=StringReplace(ReplyTemplate, '%Subject%', Data.Subject, []);

I'm checking for the utf-8 BOM and if it's found I use the Uft8Encode function.

This is all delphi7.

Thanks!
0
Steven
3/16/2010 9:55:49 PM
Reply:

Similar Artilces:

Delphi 2007 to Delphi 7
I've written a class in Delphi 2007 that is not supported in Delphi 7. What would be the best way to achive what I've done in Delphi 2007 in Delphi 7? Thanks, Tom type BondConstants = class { Bond Types } type BondType = record const TREASURY = 3; AGENCY = 0; CORP = 1; MUNI = 2; SBA = 5; MBS = 4; CMO = 6; end; { Day Count Methods } type DayCount = record const ACTUAL_360 = 2; ACTUAL_365 = 1; ACTUAL_ACTUAL = 1; d30_360 = 0; ...

Delphi 7 to Delphi XE2
Hi, Still using that old workhorse, Delphi7, but am going to the conference in London hosted by Embarcadero on Delphi XE2. Although I would like to "move with the times" and am keen to get the UNICODE and 64-bit support offered by the latest IDEs, I confess to being more than a little scared about all the UNICODE/String/AnsiString and 32/64 bit issues I'm probably going to fall over. Anyone recently upgraded from Delphi7 to one of the latest Delphi IDEs? Thanks, Alain On 03/02/2012 08:55, Alain Dekker wrote: > Still using that old workhorse, Delphi7, but...

Delphi 7 to Delphi XE
Have been using Delphi 7 for many moons ( have got later versions but never upgraded to ) My first problem is: Component Palette. in XE it is a small toolbar docked in top right in Delphi 7 it gives a large view of all the components. I am struggling to be able to cope/access my components.in Delphi XE. Can I make the component pallette tool bar the same size as Delphi 7, or is there a fast way to view/choose all available components in XE, that I have not spotted yet? Kind Regards, Robert. Hi, What I know is that in Delphi 2010 and XE you can choose between t...

I wrote an application and used cracked delphi 7, after i bought delphi 7
I wrote an application and used cracked delphi 7, I sold it, after i bought delphi 7 and recompiled application, is now my application legal or? > {quote:title=Ivan Ivanov wrote:}{quote} > I wrote an application and used cracked delphi 7, I sold it, after i bought delphi 7 and recompiled application, is now my application legal or? "I embezzled some money from my employer, returned the funds, and then got it back as a bonus. Can I still be arrested?" Uhhh .... didn't you just confess to a crime? > {quote:title=Ivan Ivanov wrote:}{quote} > I wrote an appli...

Unofficial Delphi 7 updates
Anybody noticed, even tried this Enhanced Run Time library for Delphi ? It's a hack of the D7 RTL/VCL http://blog.synopse.info/post/2009/12/20/Enhanced-Run-Time-library-for-Delphi-7 It's very interesting and I'd like to know it's stability. Edwin Ye wrote: > Anybody noticed, even tried this Enhanced Run Time library for Delphi > ? It's a hack of the D7 RTL/VCL This library violates Delphi's license (redistributing of RTL/VCL source files is not allowed). The last post was deleted and I'm sure this one will be deleted, too. -- Andreas H...

Delphi 2007 RTL in Delphi 7
I read somewhere that it is possible to use Delphi 2007's (or for that matter Delphi 2006's) RTL in Delphi 7 by just recompiling the source of RTL. Is this really possible? What are the benefits that we can derive using a higher version's RTL? TIA Yogi Yang Yogi Yang wrote: > I read somewhere that it is possible to use Delphi 2007's (or for > that matter Delphi 2006's) RTL in Delphi 7 by just recompiling the > source of RTL. If it actually compiles in Delphi 7, I guess it would be possible. OTOH, if one has Delphi 2007 already, I don't s...

Migrating from Delphi 7 to Delphi XE3
Hello, Its time to leave the old Delphi 7 and move to the new (but not the latest) XE3. I was wondering if there is a good book or reference to learn all new things XE3 added. Any suggestions? On 5/3/2013 10:17 PM, George Karatsiolis wrote: > Hello, Its time to leave the old Delphi 7 and move to the new (but not the latest) XE3. > I was wondering if there is a good book or reference to learn all new things XE3 added. Try this one for starters: http://tinyurl.com/cgsu243 Aside from that you really need to evaluate your application. XE3 is Unicode for example.... So her...

Migrating From Delphi 7 to Delphi 2010
We did not know which forum was the best for this question. We have been wanting to migrate from Delphi 7 to a later version for years but it appears to be a daunting task. We first looked at D2006 and that was a radical new world. We bought D2010 a couple of years ago and not done much with it because there are so many parts to our applications. We have done a few new applications in D2010 and they seem to work okay. D2010 has not been impressive, perhaps because of the strangeness compared to D7. Is is possible for us to do a phased sort of migration where we do a form at a time? ...

Delphi 7 to Delphi 2009 conversion
Hello group, What do I need to do to comile the following code in D2009. Compiler error after the Else E2010: Incompatible Types 'AnsiChar' and 'Char' if isUnicode then SearchHandle := FindFirstFilew( PWideChar( fn ), FindDataW ) else SearchHandle := FindFirstFile( PAnsiChar( Ansistring( fn ) ), FindDataA ); <<<<<compiler error in the above line >>>>>>>>>>> Regards, Bryan > What do I need to do to comile the following code in D2009. > Compiler error after the Else > E2010: Incompati...

Using Delphi 7 and Delphi 2010
I had a project in Delphi 7. I worked on it in Delphi 2010. When I try to open the .dpr again in Delphi 7, the IDE toolbar widens across both monitors, and Delphi 7 locks up. It appears that Delphi 2010 modifies the .dpr in a way that makes Delphi 7 unable to open it again. All my forms & units appear to open fine in D7 after D2010, except for the warnings on the ExplicitHeight and ExplicitWidth properties being missing... I can easily recreate the .dpr, but is this expected behavior? Thanks. Tom Field wrote: > I had a project in Delphi 7. > > I worked o...

Moving from Delphi 7 to Delphi 2007
Is there any compelling reason to move (quite a large project) from Delphi 7 to Delphi 2007? Specifically, is there better Vista/Win7/Win8 integration provided by Delphi 2007? (In the project I am already using a custom manifest file for Vista/Win7 etc.. and not the std XPMan component) Does 2007 add any new bugs to the scene? Thanks Rael Rael wrote: > Is there any compelling reason to move (quite a large project) from > Delphi 7 to Delphi 2007? If you are going to upgrade, why not upgrade to something newer? > In the project I am already using a custom man...

unicode in Delphi 7
Hi, I am using TntControls for some code in Delphi 7 (specifically WideCanvasTextOut). For example, Chinese displays fine on my Windows 7 (English), but there are users in China with Chinese Windows for which there are problems. What differences are there between English Windows and Chinese Windows that need to be taken into account? IOW, If the chinese is displaying correctly on English Windows, why would it not on Chinese Windows? Thanks Rael Rael Bauer wrote: > Hi, > > I am using TntControls for some code in Delphi 7 (specifically > WideCanvasTextOu...

Convert Delphi 7 to Delphi 2009 [Edit]
Hi! Please, I have a code done in Delphi 7... Now I want to convert Delphi 7 to Delphi 2009 but I'm having problems when compile the code. Error message: +[DCC Fatal Error] Far.dpr(9): F2063 Could not compile used unit 'System.pas'+ Please can somebody help me? Source Download (349KB): http://rapidshare.com/files/256191328/FileManager.rar.html Mirror Source Download: http://www.megaupload.com/?d=12GYLPT0 Password: delphi Thank u so much! (sorry for my bad english, it is not my native language) Edited by: loquax loquax on Jul 15, 2009 12:24 PM Sorry, bu...

Question about Delphi 2009 vs Delphi 7
Hi, I was wondering if any of you can point me to a URLs that show the most important improvements between these versions of Delphi. The objective is convincing the powers-that-pay-the-bills that moving to D2009 (that we'd have to purchase) is a better idea than moving to D7 (for which we have licenses). Fire at will! -- Saluti, Alessandro Federici ------------------------------- "I have been through some terrible things in my life, some of which actually happened." (Mark Twain) Alessandro Federici wrote: > Hi, > I was wondering if any of yo...