views:

201

answers:

2

Is there an easier way to convert Delphi 7 to Delphi 2009? or is there a way to use a Delphi 7 unit in a Delphi 2009 project?

I have a unit in Delphi 7 but the behavior is all messed up when I try to use it in my Delphi 2009 project.

It has a lot of differences like:

 Hangul = 'ㄱㄲㄴㄷㄸㄹㅁㅂㅃㅅㅆㅇㅈㅉㅊㅋㅌㅍㅎ' +
           'ㅏㅐㅑㅒㅓㅔㅕㅖㅗㅛㅜㅠㅡㅣ';
 ShowMessage(Copy(Hangul, 1 + (I) * 2, 2));

Shows the characters one at a time in Delphi 7 but two at a time in Delphi 2009. So I needed to change it to:

ShowMessage(Copy(Hangul, 1 + I, 1));

but that was the easiest, it get's more confusing..and the algorithm isn't mine so I can't figure out the entirety of the program.

Any help would be appreciated.

EDIT: and if anybody is interested to see the code here is the LINK. It's the unit HanInput; part. It translates keys (in english) and outputs the Korean characters. And no, I don't understand Korean.

+5  A: 

I'd have a look at the encoding of your .pas file. You can do that using Notepad++ for example. If it is anything but UTF-8, change it to UTF-8 using notepad++. That should preserve the characters and make them readable for D2009. Oh and make sure the BOM (Byte Order Mark) for UTF-8 sis included.

Not sure whether the file will then still be usable in D7 though... (Don't know when UTF-8 support was added to the IDE).


The Hangul strings in your example and the HanInput unit from your link have two bytes per character. This tells me that they are intended to be UTF-16 encoded.

This is sort of confirmed by the MultiByteToWideChar calls, even though they are used on the arguments rather than the constants. If you are actually getting UTF-16 encoded into the functions, you could get rid of that call, but you still need to find a way to deal with the constants.

Dealing with the constants - I don't see all too many in that HanInput unit - could be as simple as copying the strings to a new Ansi encoded Notepad++ file, changing its encoding to UTF-8 and then copying the strings back to your unit. You may want to ensure that you first copy all strings to a new notepad++ file, then convert them and then copy them back, as the IDE editor will probably ask you whether you want to change the units format to UTF-8 and that may mangle the constants.

Marjan Venema
The Korean characters turn into unreadable stuff like 'ĿŀŁłŃńŅņŇňʼnŊŋŌōŎŏŐőŒœ" .. :c
Dian
Am I right in assuming the original file (D7) was encoded in Ansi? In that case the string would be mangled because the string is "UTF-16" encoded within an Ansi file. When you then translate the Ansi to UTF-8, then each byte of the string will be translated separately into UTF-8 and that causes the unreadable stuff. I am having another look. What you could try in the mean time is try to declare the constants as RawByteString (look it up in the help) and then do no translation in D2009, as the compiler should take care of translating it to UTF-16LE when you assign it to a string.
Marjan Venema
Yes. The original file was encoded in Ansi. Okay, I'll do that. Thanks for the help.
Dian
@Dian, also see my update, you may only have to translate/re-encode the constants.
Marjan Venema
If it was ANSI, then you need to know what (probably Korean) codepage was used.
Warren P
I tried the copy pasting of the constants first then I redeclared the constants RawByteString but it didn't work... :C perhaps it's the indexes of the code which make me wrong?
Dian
Could be. It could also be the automatic implicit conversions the Delphi compiler will do for you. For example if a var is declared string and you assign it to an AnsiString, or vice versa, Delphi will do an implicit encoding conversion. Also the MultiByteToWideChar calls may be mangling things as they could be converting the bytes again. You may just have to step into the code and inspect vars and constants. It may help to cast them to TBytes (in the debug inspector) to get un-interpreted values and keep your sanity.
Marjan Venema
Okay. Thanks for the advice. I'll try to keep my sanity. :D
Dian
+4  A: 

In D7 Sizeof(Char) equals 1, while in D2009 it is 2. So this should help you:

ShowMessage(Copy(Hangul, 1 + (I) * Sizeof(Char), Sizeof(Char)));
Uwe Raabe
+1 for the SizeOf(Char) instead of just 2. But if the pas file is encoded in something else, not sure what would happen, though the compiler is smart enough to add constants in all formats of the strings to which they are assigned.
Marjan Venema
On the other hand, Copy doesn't work on byte count, but on character count. The OP had to change from 2 (D7) to 1 (D2009) to get it to work. And SizeOf(Char) will return 2 for any normal string in D2009, so that wouldn't help.
Marjan Venema
There are more code lines that require changing and I'm not sure if I can do that with all of them.
Dian
WHerever you code assumes sizeof(Char)=1, you must fix it.
Warren P
@warren: It's not that simple. There are a lot of lines that deal with the indexing which confuses me. Plus I don't really have the algorithm, just that code. (you can take a look for yourself if you want, the link is in my question)
Dian