Delphi XE convert string character to Ansichar

Hi

Is there a function for converting a character in a string (unicode) to an ansichar. I am upgrading some very old programs to XE that work with characters and their ordinal values. I have just been type casting them with ansistring or ansichar, but some characters are giving incorrect results.

For example

var   s : string;

begin
  s := '€';    // character is hex 80 or 128 decimal
  showmessage(inttostr(ord(s[1])));
  showmessage(inttostr(ord(ansichar(s[1]))));
  
The above give two different answers (I can understand the first), but not why the second gives the incorrect answer (172).
0
Michael
7/27/2011 5:36:18 AM
embarcadero.algorithms 211 articles. 0 followers. Follow

3 Replies
7436 Views

Similar Articles

[PageSpeed] 23

Am 27.07.2011 07:36, schrieb Michael Cashion:
> Hi
>
> Is there a function for converting a character in a string (unicode) to an ansichar. I am upgrading some very old programs to XE that work with characters and their ordinal values. I have just been type casting them with ansistring or ansichar, but some characters are giving incorrect results.
>
> For example
>
> var   s : string;
>
> begin
>    s := '€';    // character is hex 80 or 128 decimal
>    showmessage(inttostr(ord(s[1])));
>    showmessage(inttostr(ord(ansichar(s[1]))));
>
> The above give two different answers (I can understand the first), but not why the second gives the incorrect answer (172).

You might succeed with

showmessage(inttostr(ord(ansistring(s)[1])));

This will convert the Unicode string to an Ansistring before accessing 
the individual char.

Uwe Raabe
-- 
Uwe's Blog: The Art of Delphi Programming <http://www.uweraabe.de/>
0
Uwe
7/27/2011 6:27:39 AM
> {quote:title=Uwe Raabe wrote:}{quote}
> Am 27.07.2011 07:36, schrieb Michael Cashion:
> > Hi
> >
> > Is there a function for converting a character in a string (unicode) to an ansichar. I am upgrading some very old programs to XE that work with characters and their ordinal values. I have just been type casting them with ansistring or ansichar, but some characters are giving incorrect results.
> >
> > For example
> >
> > var   s : string;
> >
> > begin
> >    s := '€';    // character is hex 80 or 128 decimal
> >    showmessage(inttostr(ord(s[1])));
> >    showmessage(inttostr(ord(ansichar(s[1]))));
> >
> > The above give two different answers (I can understand the first), but not why the second gives the incorrect answer (172).
> 
> You might succeed with
> 
> showmessage(inttostr(ord(ansistring(s)[1])));
> 
> This will convert the Unicode string to an Ansistring before accessing 
> the individual char.
> 
> Uwe Raabe
> -- 
> Uwe's Blog: The Art of Delphi Programming <http://www.uweraabe.de/>

Thanks,  but that does not work in all cases

For example
   ansistring(chr(133)) gives you a "?"  rather than the correct value, in fact I get a "?" for any chr value over 130

I also tried rawbytestring(chr(133)) for the same incorrect result

Edited by: Michael Cashion on Jul 28, 2011 11:19 PM
0
Michael
7/29/2011 6:25:05 AM
Am 29.07.2011 08:25, schrieb Michael Cashion:
>     ansistring(chr(133)) gives you a "?"  rather than the correct value, in fact I get a "?" for any chr value over 130

Depends on what you call "correct". (Besided this being a complete 
different question than your previous one)

Chr(133) will give you the Unicode character represented by the number 
133 (which btw. is not defined). So the "?" is absolute correct here.

What you expect is the Ansi character represented by the number 133, 
which depends heavily on the codepage used. AnsiChar(133) will give you 
the correct result here (depending on the current codepage).

If you really have to work with AnsiStrings you should declare your 
string variables as such, but be aware of the codepage problem. In 
addition have a close look at the warnings for implicit conversion 
between AnsiString and string which are emitted by the compiler.

You can even declare your own AnsiString type bound to a specific 
codepage. For example

type
   TMyAnsiString = type AnsiString(1252);

declares an AnsiString with codepage 1252.

Uwe Raabe
-- 
Uwe's Blog: The Art of Delphi Programming <http://www.uweraabe.de/>
0
Uwe
7/30/2011 8:30:21 AM
Reply:

Similar Artilces:

Delphi XE
I need to store an Ansi character (something between #32 and #255) in a string (UnicodeString, it's Delphi XE). And storing this character fails on Windows XP, while it works on Win7. To duplicate the problem, run the following program, compiled with Delphi XE on Win7 and Windows XP: procedure TForm6.Button1Click(Sender: TObject); var s: string; i: integer; begin s := AnsiChar(252); showmessage(s + ' ' + inttostr(ord(s[1]))); //result: 252 i := 252; s := AnsiChar(i); showmessage(s + ' ' + inttostr(ord(s[1]))); //result: 1100 on ...

Delphi 7.0 code convert to delphi XE ...
Hello All, I create an application using Web Server Application then CGI stand alone... In WebModule I add ModuleController component and IWPageProcedure... Below is my code in Delphi 7.0, this is how to call another units in webmodule... .... .... procedure TWebModule1.proMainGetForm(ASender: TIWPageProducer; AWebApplication: TIWApplication; var VForm: TIWPageForm); begin VForm := TfrmMain.Create(AWebApplication); end; procedure TWebModule1.proLogInGetForm(ASender: TIWPageProducer; AWebApplication: TIWApplication; var VForm: TIWPageForm); begin VForm := TfrmLogIn.Create(AWebA...

Delphi 5 (string[80]) vs Delphi XE
Hi guys i'm trying to convert a old application made with Delphi 5 to Delphi XE 5 The probleme i have right now is about String[80] and the Read / Write procedure i have a type like that Type TEnrMess = record Mess : String[80]; MessSuiv : Word; Hint : Word; end; And i read/write this type to a File of TEnrMess My probleme is that the string have changed from Delphi 5 to Delphi XE i join the link of a simple of the probleme that can be compiled on delphi 5 and any Delphi XE https://mega.co.nz/#!wV1VyZzY!1ha5oOxTg7L1WJAOXutUygkj...

IBX (Delphi XE), Interbase XE and character set UTF8
Hello, I'm trying to get Interbase and IBX running with an UTF8 database. I'm using Delphi XE and Interbase XE. I'm creating the database like this: var aIBDatabase: TIBDatabase; ... aIBDatabase.DatabaseName := 'test_utf8.ib' aIBDatabase.Params.Clear; aIBDatabase.Params.Add('PAGE_SIZE 4096'); aIBDatabase.Params.Add('DEFAULT CHARACTER SET utf8'); aIBDatabase.CreateDatabase; The fields in the tables were created as VARCHAR. The database really seems to use character set utf8, because with IBExpert I'm able to set and r...

Error converting characters into server's character set. Some character(s) could not be converted.
Error converting characters into server's character set. Some character(s) could not be converted. No changes made to database. UPDATE Ticket_detail SET problem = 'mecho Team, I have set up several table repdefs in our test environment (PROD_jam) to determine the benefit of having table repdefs in addition to a database repdef. After I set up one of the table repdefs I ran several updates on the primary table to verify whether the where-clause was smaller on the replicate database. When I reviewed the transaction in the outbound queue on the secondary rep server (T...

converting string to a character
Hi, I have a little problem when I convert from string to a character.  I've tried using convert function but doesn't really work well.  I then tried  using Char.Parse(string) function but that's giving me wrong conversion.  I have the following in C# char Season1 = Char.Parse(Season.Text); The Season.Text only contains a single character (i.e. F, W, S,..)  When I ran through the debugger with Season.Text value W, the Char.Parse(Season.Text) returned 87 'W'.  Any ideas as to how to get just 'W'? Thanks,   87 is the ASCII code for W- I think that it ...

Best Place to get help on converting Delphi 2007 string to Delphi 2009
Where is the best place to get help on converting Delphi 2007 strings to Delphi 2009? The most common problem getting Delphi 7-Delphi 2007 components to compile seems to be caused by the new Unicode strings in Delphi 2009. It would be great if there was some place to go to get help with this. Maybe there should be a new group for String conversion where developers could ask short questions about conversion. For example: Delphi 7 - Delphi 2007 CharArrayPtr = ^CharArray; CharArray = array[0..MaxInt-1] of Char; Compiles Delphi 2009 CharArrayPtr = ^CharArray; CharArray ...

FW: searching for a string of characters after another string of characters
If i understand you correctly, you're trying to extract exactly one line out of a file. I would do this: while (<IN>) { if (/^hostname\s+(\S+)$/) # or some other regex derivative { $hostname=$1; last; } } -----Original Message----- From: Grossner, Tim X. (AIT) [mailto:tg1761@sbc.com] Sent: Thursday, July 12, 2001 3:03 PM To: beginners@perl.org Subject: searching for a string of characters after another string of characters How would I do this: file A contains a line of characters always looking like "hostname XXXXX". I want to take the...

RE: searching for a string of characters after another string of characters
Reading the entire file into an array is rarely a good idea, but if you insist on doing it, then you have to realize that each element of the array is going to be exactly one line of the file. So, "hostname XXXXX\n" is going to be one element unless you explicitly extract using a split or regex. -----Original Message----- From: Grossner, Tim X. (AIT) [mailto:tg1761@sbc.com] Sent: Thursday, July 12, 2001 3:28 PM To: Mooney Christophe-CMOONEY1 Subject: RE: searching for a string of characters after another string of characters No, I have already extracted the line, an...

converting datetime from character string
I read as many previous posts on this subject before posting this but I'm stuck.I have a form that has a from and to date range for a search page.I think my problem is taking place in my server validition procedure.I'm trying to pass a date value typed in a textbox to a stored procedure that is expecting a datetime data type.Here is two of my server validation procedures (They are all pretty much the same):  Sub ValidateFromRepoDt(ByVal sender As Object, _ ByVal args As ServerValidateEventArgs) Try 'make sure there is a value...

Converting from Delphi 2007 to XE
I've read enough to get confused... I want to convert some units from D2007 to XE , I DO NOT intend to support Unicode. So... If I change all the declarations... Char to AnsiChar PChar to PAnsiChar String to AnsiString am I on the right track ? What other problems should I anticipate ? Thanks JT > If I change all the declarations... > > Char to AnsiChar > PChar to PAnsiChar > String to AnsiString > > am I on the right track ? No. Keep everything Char, PChar and string as much as possible. The D2009+ AnsiString isn't the sam...

Converting datetime from character string
Hi there, I have the following code: Dim CityTown As String = Ctype(Request.Querystring("CityTown"), String) Dim Suburb As String = Ctype(Request.Querystring("Suburb"), String) Dim SuburbValue As String = Ctype(Request.Querystring("Suburb"), String) Dim Rooms As String = Ctype(Request.Querystring("Rooms"), String) Dim Rent As String = Ctype(Request.Querystring("Rent"), String) Dim DateToday = DateTime.Now Dim mySQL AS String If suburbValue = "- All -" Then mySQL = &qu...

convert ascii html characters to string
I am passing a string from javascript to asp.net code and it converts all spaces to %20, commas to some other characters and so on.... Is there a way to parse that string in asp.net to get the real characters. Thanks. Igor Server.UrlDecode or Server.HtmlDecode I'm not sure which encoding u are using, but one of these will do the trick.Christian CalderonNeed to work in Classic ASP? then do it the right way with CLASP!Classic ASP Framework...

How do I parse a string character by character?
Hi, I have a string i.e) 156.5 I need to be able to parse the string one character at a time and write the character value to an array Pseudo Code: For i=1 To String.Length strImageArray(i) = stringCharacter Next Hope this makes sense! Thanks, Chris TrystrImageArray = myString.ToCharArray...

Web resources about - Delphi XE convert string character to Ansichar - embarcadero.algorithms

List of Killzone characters - Wikipedia, the free encyclopedia
Cpl. Dante Garza is a loyal, optimistic and likable character- an effective team player. A close companion of Sev, the pair have served together ...

Why Coke Is Sticking With These Giggling Characters For Chinese New Year
... it wanted to find a symbol that would have that same long-term appeal in China, so it went back to the archives. It brought out some characters ...

Sina Weibo ends 140-character limit ahead of Twitter
Chinese micro-blogging service Sina Weibo beats its US rival Twitter to ditching a 140-character limit on posts.

Cut ‘Star Wars: The Force Awakens’ scene reveals a new character’s major powers
... one of the many Star Wars movies that will hit cinemas by the end of this decade. One of the best things about the movie was its new characters ...

Guilty Gear Xrd: Revelator is getting yet another new character in Kum Haehyun
... a triumphant return in the near future, it looks like there's a newcomer on the block for Guilty Gear Xrd: Revelator Kum Haehyun. this character ...

'Mortal Kombat XL' is loaded with characters and skin packs
NetherRealm's found a new way to milk its beloved creation Mortal Kombat X. Today, the studio revealed a version of the title dubbed Mortal Kombat ...

Weibo Beats Twitter To Raising Its 140-Character Limit
The Chinese microblogging service is raising its posting limit to 2,000 characters China's Twitter, Weibo, has beat the original to the punch ...

You Can Now Download Black Desert Online's Famed Character Creator For Free
... made a splash in the video game world not because of particularly impressive gameplay footage, but because of its particularly impressive character ...

11 Ways to Write a Character-Focused Story That Still Feels Action-Packed
... we love books that are “page-turners.” But does a fast-paced story full of thrills and excitement mean you can’t also make room for character? ...

‘Street Fighter 5’ Arriving With A Prologue & 16 Playable Characters Come Release Date
... Fighter 5” producer Yoshinori Ono with Hadoken, the game, come its February release date will include a prologue to better introduce the characters. ...

Resources last updated: 1/22/2016 3:33:10 AM