Convert String to Int
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngfreeforall' list.


Ken Mingeaud
I need to convert a string of 1234 to an INT of 1234 so I can use it a
vbscript sql query. Got clues?
Reply to this message...
 
    
Ian Vink
Int.Parse()

-----Original Message-----
From: Ken Mingeaud [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 1:05 PM
To: aspngfreeforall
Subject: [aspngfreeforall] Convert String to Int

I need to convert a string of 1234 to an INT of 1234 so I can use it a
vbscript sql query. Got clues?

| ASP.net DOCS = http://www.aspng.com/docs | [aspngfreeforall] member
Click here to reveal e-mail address = YOUR ID |
http://www.asplists.com/aspngfreeforall = JOIN/QUIT |
news://ls.asplists.com = NEWSGROUP
Reply to this message...
 
    
TClamp@Welcom.com
Although this may not be the most efficient way, you may use the Convert
class to perform conversion of simple types.

Convert.ToInt16(someString)

Does anyone else know of a better way?

Tim Clamp
Welcom Software Technology
http://www.welcom.com <http://www.welcom.com>
Click here to reveal e-mail address <mailto:Click here to reveal e-mail address>

-----Original Message-----
From: Ken Mingeaud [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 12:05 PM
To: aspngfreeforall
Subject: [aspngfreeforall] Convert String to Int

I need to convert a string of 1234 to an INT of 1234 so I can use it a
vbscript sql query. Got clues?

| ASP.net DOCS = http://www.aspng.com/docs | [aspngfreeforall] member
Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/aspngfreeforall =
JOIN/QUIT | news://ls.asplists.com = NEWSGROUP
Reply to this message...
 
    
Steve Mark
See the "Convert" class.

Steve
-----Original Message-----
From: Ken Mingeaud [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 10:05 AM
To: aspngfreeforall
Subject: [aspngfreeforall] Convert String to Int

I need to convert a string of 1234 to an INT of 1234 so I can use it a
vbscript sql query. Got clues?
| ASP.net DOCS = http://www.aspng.com/docs | [aspngfreeforall] member
Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/aspngfreeforall JOIN/QUIT | news://ls.asplists.com = NEWSGROUP
Reply to this message...
 
    
chris
Int32.Parse("1234") is what Convert.ToInt32() calls behind the scenes. (with
strings anyway)

-Chris Frazier
.NET Solution Developer
Velocity Databank, Inc.
www.velocitydatabank.com

-----Original Message-----
From: Ian Vink [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 12:11 PM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Convert String to Int

Int.Parse()

-----Original Message-----
From: Ken Mingeaud [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 1:05 PM
To: aspngfreeforall
Subject: [aspngfreeforall] Convert String to Int

I need to convert a string of 1234 to an INT of 1234 so I can use it a
vbscript sql query. Got clues?

| ASP.net DOCS = http://www.aspng.com/docs | [aspngfreeforall] member
Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/aspngfreeforall JOIN/QUIT | news://ls.asplists.com = NEWSGROUP

| ASP.net DOCS = http://www.aspng.com/docs | [aspngfreeforall] member
Click here to reveal e-mail address = YOUR ID |
http://www.asplists.com/aspngfreeforall = JOIN/QUIT | news://ls.asplists.com
= NEWSGROUP
Reply to this message...
 
    
Thomas Fuller
I may be talking VB and that's why I'm suggesting this but why not use
intTemp = CTYPE("1234",int32)

Tom Fuller

-----Original Message-----
From: chris [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 1:26 PM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Convert String to Int

Int32.Parse("1234") is what Convert.ToInt32() calls behind the scenes. (with
strings anyway)

-Chris Frazier
.NET Solution Developer
Velocity Databank, Inc.
www.velocitydatabank.com
-----Original Message-----
From: Ian Vink [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 12:11 PM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Convert String to Int
Int.Parse()

-----Original Message-----
From: Ken Mingeaud [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 1:05 PM
To: aspngfreeforall
Subject: [aspngfreeforall] Convert String to Int

I need to convert a string of 1234 to an INT of 1234 so I can use it a
vbscript sql query. Got clues?
| ASP.net DOCS = http://www.aspng.com/docs | [aspngfreeforall] member
Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/aspngfreeforall JOIN/QUIT | news://ls.asplists.com = NEWSGROUP
| ASP.net DOCS = http://www.aspng.com/docs | [aspngfreeforall] member
Click here to reveal e-mail address = YOUR ID |
http://www.asplists.com/aspngfreeforall = JOIN/QUIT | news://ls.asplists.com
= NEWSGROUP
| ASP.net DOCS = http://www.aspng.com/docs | [aspngfreeforall] member
Click here to reveal e-mail address = YOUR ID | http://www.asplists.com/aspngfreeforall JOIN/QUIT | news://ls.asplists.com = NEWSGROUP
Reply to this message...
 
    
Moore, Matthew
Just for grins I threw these two lines of code to see how it compiled and it appears that they both run almost identical as Chris notes below with strings.

int lame = int.Parse("1234");
int lame2 = Convert.ToInt32("1234");

IL_0006: ldstr "1234"
IL_000b: call int32 [mscorlib]System.Int32::Parse(string)
IL_0010: stloc.0
IL_0011: ldstr "1234"
IL_0016: call int32 [mscorlib]System.Convert::ToInt32(string)
IL_001b: stloc.1

Matt
-----Original Message-----
From: chris [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 1:26 PM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Convert String to Int

Int32.Parse("1234") is what Convert.ToInt32() calls behind the scenes. (with strings anyway)

-Chris Frazier
.NET Solution Developer
Velocity Databank, Inc.
www.velocitydatabank.com

Reply to this message...
 
    
Feduke Cntr Charles R
[Original message clipped]

    Yes, but the real performance question is:

Does

> int32 [mscorlib]System.Int32::Parse(string)

call

> int32 [mscorlib]System.Convert::ToInt32(string)

?

- Chuck

-----Original Message-----
From: Moore, Matthew [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 1:51 PM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Convert String to Int

Just for grins I threw these two lines of code to see how it compiled and it
appears that they both run almost identical as Chris notes below with
strings.

int lame = int.Parse("1234");
int lame2 = Convert.ToInt32("1234");

IL_0006: ldstr "1234"
IL_000b: call int32 [mscorlib]System.Int32::Parse(string)
IL_0010: stloc.0
IL_0011: ldstr "1234"
IL_0016: call int32 [mscorlib]System.Convert::ToInt32(string)
IL_001b: stloc.1

Matt
-----Original Message-----
From: chris [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 1:26 PM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Convert String to Int

Int32.Parse("1234") is what Convert.ToInt32() calls behind the scenes. (with
strings anyway)

-Chris Frazier
.NET Solution Developer
Velocity Databank, Inc.
www.velocitydatabank.com

| ASP.net DOCS = http://www.aspng.com/docs
| [aspngfreeforall] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/aspngfreeforall = JOIN/QUIT
| news://ls.asplists.com = NEWSGROUP

Reply to this message...
 
    
Moore, Matthew
Well.... Here is what you wanted. :)

System.Convert.ToInt32(string) is

public static int ToInt32(string value) {
    if (value == null)
        return 0;
    return Int32.Parse(value);
}

and

System.Int32.Parse is

public static int Parse(string s) {
    return Int32.Parse(s, 7, null);
}

So we now know for sure that the Convert.ToInt32(string) calls Int32.Parse(value), so I would assume it is more efficient to use Int32.Parse instead of Convert.ToInt32().

Matthew T. Moore
Software Engineer - AUTEC
561.832.8566 x7292
Click here to reveal e-mail address

-----Original Message-----
From: Feduke Cntr Charles R [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 2:13 PM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Convert String to Int

[Original message clipped]

    Yes, but the real performance question is:

Does

> int32 [mscorlib]System.Int32::Parse(string)

call

> int32 [mscorlib]System.Convert::ToInt32(string)

?

- Chuck

-----Original Message-----
From: Moore, Matthew [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 1:51 PM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Convert String to Int

Just for grins I threw these two lines of code to see how it compiled and it
appears that they both run almost identical as Chris notes below with
strings.

int lame = int.Parse("1234");
int lame2 = Convert.ToInt32("1234");

IL_0006: ldstr "1234"
IL_000b: call int32 [mscorlib]System.Int32::Parse(string)
IL_0010: stloc.0
IL_0011: ldstr "1234"
IL_0016: call int32 [mscorlib]System.Convert::ToInt32(string)
IL_001b: stloc.1

Matt

Reply to this message...
 
    
Moore, Matthew
On a side note, I just wrote a benchmarking program to compare the two. I did 100,000 conversions with int.Parse(string) and Convert.ToInt32(string).

Parse completed it in 9.183 seconds.
Convert completed it in 9.283 seconds.

Yes Parse is faster, but if a program is running slow, I'd suggest looking for the bottleneck somewhere else.

Matt

-----Original Message-----
From: Moore, Matthew [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 3:02 PM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Convert String to Int

Well.... Here is what you wanted. :)

System.Convert.ToInt32(string) is

public static int ToInt32(string value) {
    if (value == null)
        return 0;
    return Int32.Parse(value);
}

and

System.Int32.Parse is

public static int Parse(string s) {
    return Int32.Parse(s, 7, null);
}

So we now know for sure that the Convert.ToInt32(string) calls Int32.Parse(value), so I would assume it is more efficient to use Int32.Parse instead of Convert.ToInt32().

Matthew T. Moore
Software Engineer - AUTEC
561.832.8566 x7292
Click here to reveal e-mail address

-----Original Message-----
From: Feduke Cntr Charles R [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 2:13 PM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Convert String to Int

[Original message clipped]

    Yes, but the real performance question is:

Does

> int32 [mscorlib]System.Int32::Parse(string)

call

> int32 [mscorlib]System.Convert::ToInt32(string)

?

- Chuck

-----Original Message-----
From: Moore, Matthew [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 1:51 PM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Convert String to Int

Just for grins I threw these two lines of code to see how it compiled and it
appears that they both run almost identical as Chris notes below with
strings.

int lame = int.Parse("1234");
int lame2 = Convert.ToInt32("1234");

IL_0006: ldstr "1234"
IL_000b: call int32 [mscorlib]System.Int32::Parse(string)
IL_0010: stloc.0
IL_0011: ldstr "1234"
IL_0016: call int32 [mscorlib]System.Convert::ToInt32(string)
IL_001b: stloc.1

Matt

| ASP.net DOCS = http://www.aspng.com/docs
| [aspngfreeforall] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/aspngfreeforall = JOIN/QUIT
| news://ls.asplists.com = NEWSGROUP

Reply to this message...
 
    
chris
I've debugged using the Convert.ToInt32() method, and the stack trace showed
that on a string, it does call Int32.Parse(), so - the other way around.

-Chris Frazier
.NET Solution Developer
Velocity Databank, Inc.
www.velocitydatabank.com

-----Original Message-----
From: Feduke Cntr Charles R [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 1:13 PM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Convert String to Int

[Original message clipped]

    Yes, but the real performance question is:

Does

> int32 [mscorlib]System.Int32::Parse(string)

call

> int32 [mscorlib]System.Convert::ToInt32(string)

?

- Chuck

-----Original Message-----
From: Moore, Matthew [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 1:51 PM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Convert String to Int

Just for grins I threw these two lines of code to see how it compiled and it
appears that they both run almost identical as Chris notes below with
strings.

int lame = int.Parse("1234");
int lame2 = Convert.ToInt32("1234");

IL_0006: ldstr "1234"
IL_000b: call int32 [mscorlib]System.Int32::Parse(string)
IL_0010: stloc.0
IL_0011: ldstr "1234"
IL_0016: call int32 [mscorlib]System.Convert::ToInt32(string)
IL_001b: stloc.1

Matt
-----Original Message-----
From: chris [mailto:Click here to reveal e-mail address]
Sent: Wednesday, August 07, 2002 1:26 PM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Convert String to Int

Int32.Parse("1234") is what Convert.ToInt32() calls behind the scenes. (with
strings anyway)

-Chris Frazier
.NET Solution Developer
Velocity Databank, Inc.
www.velocitydatabank.com

| ASP.net DOCS = http://www.aspng.com/docs
| [aspngfreeforall] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/aspngfreeforall = JOIN/QUIT
| news://ls.asplists.com = NEWSGROUP

| ASP.net DOCS = http://www.aspng.com/docs
| [aspngfreeforall] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/aspngfreeforall = JOIN/QUIT
| news://ls.asplists.com = NEWSGROUP

Reply to this message...
 
    
Alex Lowe
Guys,

For future reference, please hold this kind of thread on [aspngspeed]
(http://www.aspfriends.com/aspfriends/aspngspeed.asp) where these
discussion are on topic and many optimization experts hang out.

Regards,

Alex - AspFriends.com Moderation Team
Microsoft MVP - ASP.NET

***********************************************************
Translate C# code to VB.NET code at
http://aspalliance.com/aldotnet/examples/translate.aspx
***********************************************************

[Original message clipped]

Reply to this message...
 
 
System.Convert
System.Int32




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com