I am running a legacy application, built on Delphi2007, where we used to handle non-English characters by storing 2byte Hex code of the character in the DB. While reading we apply char() to convert these Hex code to String.
String to Hex (before saving to DB):
strHex := Format( '%x', [ Byte( strText[ lIndex ] ) shr 4 ] );
DataStr[ lPos ] := strHex[ 1 ];
inc( lPos );
strHex := Format( '%x', [ Byte( strText[ lIndex ] ) and $0F ] );
DataStr[ lPos ] := strHex[ 1 ];
inc( lPos );
//in simple I am saving the Hex code to pcData
Hex to String (after reading from DB):
strText := strText + Chr( StrToInt('$'+ DataStr[lPos] + DataStr[lPos + 1]))
This code started breaking after moving to Delphi XE7, where string is treated as UniCode String, we explicitly have to convert the string to AnsiString type.
Converting below string to hex
ТуцЕфылАшдеук8311
In Delphi 2007 gives:
\D2\F3\F6\C5\F4\FB\EB\C0\F8\E4\E5\F3\EA8311
In Delphi XE7 gives:
\22\43\46\1A\33\4B\4B\48\44\42\14\44\49\33\351522
I would like to know the best way I can modify this code such that I can handle my legacy data.
AnsiStringof this type and use it when retrieving data from your database. I would strongly suggest that you then immediately convert it to a normal (unicode)stringwhen using it in your application, converting back only to store in the DB. Better yet, update your DB to unicode and get rid of this mess altogether. See : stackoverflow.com/a/7222871/327083 - J...