Board index » cppbuilder » Re: Convert AnsiString to integer

Re: Convert AnsiString to integer


2003-07-07 08:10:13 AM
cppbuilder70
tnx
"nicolasr" <nicolasrNOSPAMATSIGNgmx.net>ha scritto nel messaggio
Quote
If I understand correctly you need conversion from AnsiString to int?
You can use for example:

AnsiString s = "17";

int i = StrToInt(s);

or

int i = s.ToInt();

both will throw an exception if the AnsiString does not contain a
valid number!

regards,
Nick


 
 

Re:Re: Convert AnsiString to integer

"nicolasr" <nicolasrNOSPAMATSIGNgmx.net>wrote in message
Quote
both will throw an exception if the AnsiString
does not contain a valid number!
In which case you can use StrToIntDef() or ToIntDef() instead, or else
manage the exception handling manually.
Gambit
 

Re:Re: Convert AnsiString to integer

"nicolasr" <nicolasrNOSPAMATSIGNgmx.net>wrote:
Quote
int i = StrToInt(s);
int i = s.ToInt();
both will throw an exception if the AnsiString does not
contain a valid number!
You can use TryStrToInt instead to avoid this. However, I guess
this function is not available in every version of BCB.
 

{smallsort}

Re:Re: Convert AnsiString to integer

"Thorsten Kettner" < XXXX@XXXXX.COM >wrote:
Quote

"nicolasr" <nicolasrNOSPAMATSIGNgmx.net>wrote:
>int i = StrToInt(s);
>int i = s.ToInt();
>both will throw an exception if the AnsiString does not
>contain a valid number!

You can use TryStrToInt instead to avoid this. However, I guess
this function is not available in every version of BCB.
atoi converts a string to an int. If the string can not be
converted to an int it returns 0. It will work even if the
string is '1a'. The first unrecognized character ends the
conversion.
int i = atoi( s.c_str() );
~ JD
 

Re:Re: Convert AnsiString to integer

Hi,
I tried your code and got the error "'21' is not a valid component
name" (translated from german)
Looking at the VCL sources I found that the name property is validated
by a function called IsValidIdent. The following comment precedes the
declaration of this function:
{ IsValidIdent returns true if the given string is a valid identifier.
An
identifier is defined as a character from the set ['A'..'Z',
'a'..'z', '_']
followed by zero or more characters from the set ['A'..'Z',
'a'..'z',
'0..'9', '_']. }
So how about using names like CB1, CB2 or similar? Or assign your
numerical identifier to the Tag property instead of the name.
hope this helps,
Nick