Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Programming Talk / Delphi - how to convert string to integer and integer to string?

Author
Message
Sergey K
21
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 20th Dec 2005 15:56
hey all, how do i convert string to integer and integer to string?
in dbp its like this string = str$(int) and integer = val(string$)

hyrichter
21
Years of Service
User Offline
Joined: 15th Feb 2004
Location: Arizona
Posted: 20th Dec 2005 17:47 Edited at: 20th Dec 2005 17:49
Converting an integer to a string:


Converting a string to an integer:


Or you can use the StrToIntDef function which uses a default value if the string is unable to be converted to an integer.


edit: Delphi is a very strongly typed language. You can't even show a message box with an integer value unless you first convert it to a string. Just hit ctrl + Space if you're unsure of what command to use to convert it. They're usually pretty easy to guess.

Sergey K
21
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 20th Dec 2005 19:09 Edited at: 20th Dec 2005 19:14
errr ok thx.. but i still didnt got that part of StrToIntDef..

can u explain lil bit more of it?

[edit]
i tryed the CTRL + Space and typed IntToStr, it founded the command, but when i tryed to type StrToInt, there was no result.

Darkbasic MADPSP
19
Years of Service
User Offline
Joined: 15th Jun 2005
Location: Uk
Posted: 20th Dec 2005 19:21
delphi isn't that like dbpro?

Experienced DB http://www.greatgames3d.com (work in progress site)
Sergey K
21
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 20th Dec 2005 19:29
@Darkbasic MAD: lol no. its got a power of C++

empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 21st Dec 2005 00:19
Quote: "but i still didnt got that part of StrToIntDef."

If the StrToInt function can't convert the string to an integer it'll raise an exception (an error message will appear). When you use StrToIntDef instead, Delphi will use the specified default value. Example:





Quote: "delphi isn't that like dbpro?"

No Delphi is a variant of Object Pascal. It's probably easier to understand for Basic users than C or C++ (and I'd say even C#) cause modern Basic languages adopted a lot of elements and keywords from Pascal.
Delphi is RAD tool similar to VB and the compiler produces extremely optimised machine code. So the executables are stupendous fast.


Play Nice! Play Basic! Version 1.089
Sergey K
21
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 21st Dec 2005 12:07
ok thx empty i think i understood that part.
does there is in Delphi a command that called Try/Catch/End Try like in VB?

cuz this command will catch any crash in visual basic.. does there is any command like this in Delphi that can catch crashes?

empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 21st Dec 2005 23:47
Yeah there's Try/Except/Finally in Delphi.
Try
// do error prone stuff here
Except
// do stuff only if an error occured
End;

or

Try
// do error prone stuff here
Finally
// do stuff no matter if an error occured or not.
// useful when objects need to be freed etc.
End;

There are more ways to catch errors and exceptions, though.


Play Nice! Play Basic! Version 1.089
MicroMan
21
Years of Service
User Offline
Joined: 19th Aug 2003
Location: Stockholm, Sweden
Posted: 22nd Dec 2005 21:35
Quote: "errr ok thx.. but i still didnt got that part of StrToIntDef.."


Delphi Help is an excellently, and apparently underrated, resource for explanations and definitions about lots of things...

-----
They SAID that given enough time a million monkeys with typewriters could recreate the collected works of William Shakespeare... Internet sure proved them wrong.
-----
empty
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: 3 boats down from the candy
Posted: 22nd Dec 2005 22:00
...although it got a little worse after Delphi 7.


Play Nice! Play Basic! Version 1.089

Login to post a reply

Server time is: 2025-05-18 14:24:00
Your offset time is: 2025-05-18 14:24:00