12,304,884 members (65,806 online)
Rate this:
See more:
hi how to convert string to integer?

my string was like this "txtstring.text"

i want to convert "txtstring.text" to int64.
txtstring.text having the value like this 1:00:51:231 like this
i want to convert this time (1:00:51:231) to in milliseconds.
Posted 30-May-12 22:34pm
Edited 30-May-12 23:49pm
Prosan8.9K
v3
Rajesh Kariyavula 31-May-12 4:49am

Is that a time that you want to convert.
sandeep nagabhairava 31-May-12 4:55am

ya rajesh. time which is in string format,i want convert that to int form ... thanks for past answer..
Rajesh Kariyavula 31-May-12 5:00am

Sandeep, why do you want to convert time to an integer, is it for the previous calculation for getting amount based on time??
sandeep nagabhairava 31-May-12 5:26am

yes.. exactly rajesh...
Rajesh Kariyavula 31-May-12 5:48am

You dont need to convert time to an integer for this, I have solved this for you, please check my answer.
Rajesh Kariyavula 31-May-12 6:21am

Let me know if that doesn't suffice your need.
sandeep nagabhairava 31-May-12 6:39am

thank you for caring rajesh. i solved my self...
Rajesh Kariyavula 31-May-12 13:57pm

thats great Sandeep
sandeep nagabhairava 31-May-12 14:00pm

thank u...:)

Rate this:

## Solution 2

Hello,
try following-
```static public string makeNumeric( string str, bool decimals )
{
bool hasdec = false;
string ret = "";
for(int i=0; i<= str.Length-1; i++)
{
if(Char.IsDigit(str, i))
{
ret += str[i];
}
else if( decimals && str[i] == ':' && !hasdec )
{
ret += str[i];
hasdec = true;
}
}
return ret;
}```

call this function and assign the result to Integer variable. if any problem arises ask me.
v3
sandeep nagabhairava 31-May-12 4:45am

it's not working, my string was like this txtstring.text having the value like this 1:00:51:231.
Rate this:

## Solution 3

Every numeric type in .Net has Parse and TryParse methods.
Use them for conversion of numbers represented as strings to their numeric values.

```long myNum = long.Parse(txtstring.text);

or

long.TryParse(txtstring.text, out myNum );```
sandeep nagabhairava 31-May-12 4:45am

it's not working, my string was like this txtstring.text having the value like this 1:00:51:231.
Pete O'Hanlon 31-May-12 4:49am

That's not a valid number, you can't parse that as it stands. You'd have to remove the : values.
sandeep nagabhairava 31-May-12 4:51am

it's not a number, it's time. how can we convert it into int form.
Rate this:

Rate this:

## Solution 5

I assume that you are talking about taking the contents of txtString.Text and converting it to an integer. There are a few ways that you can achieve this, but the way that's usually recommended is to use the TryParse method of Int64. This is what it would look like:
```public static Int64 GetValue(this string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return 0;
}
Int64 output;
// TryParse returns a boolean to indicate whether or not
// the parse succeeded. If it didn't, you may want to take
// some action here.
Int64.TryParse(value, out output);
return output;
}```
VJ Reddy 31-May-12 4:59am

Good point to use TryParse. 5!
Rate this:

## Solution 6

It seems from the question that the `Text `value contains `:` characters and the numeric value is required by eliminating these characters. If that is the case then the following code may be useful
```long numValue;
string text ="1:00:51:231";
Int64.TryParse(text.Replace(":",""),out numValue);```

As seen from the `comment of OP `to another solution the text
`1:00:51:231`
represents time. In that case the string can be converted into `milli seconds` to generate a long number as shown below:
```string text = "1:00:51:231";
long[] multipliers = new long[] {3600000,60000,1000,1};
int i=0;
var longTime = text.Split(':').Aggregate (0,(long total, string part) =>
total += Int64.Parse(part) * multipliers[i++]);
//longTime = 3651231
```
v3
Pete O'Hanlon 31-May-12 5:18am

I like this solution, but I'm not sure the OP will be able to understand it. Gets my 5 though.
VJ Reddy 31-May-12 5:22am

Thank you, Pete :)
losmac 31-May-12 9:51am

No comment, just BIG 5!
VJ Reddy 31-May-12 10:39am

Thank you, losmac :)
Rate this:

## Solution 7

OK, seeing that your question changed from the version we originally answered, here's my take.

"txtstring.text having the value like this 1:00:51:231 like this"

First of all, you should name things better. There is no way to tell, from txtstring, what it's intended for. This is why no answer thought it was the time component.

Second, you state this is a time component. OK, why are you wanting to change it into a number? As it stands, time components generally make little sense if they are not coupled with a date. If you had the date and time, you could use `DateTime.TryParseExact` on this and extract the `Tick` count.

Third, if you are only interested in the time part, there's no real need to store it in an `Int64`. You can simply retrieve the milliseconds from midnight, which is an integer. Here's a quick hacky version that would do this:
```private static int Parse(string time)
{
DateTime date = DateTime.Parse("2000-01-01 00:00:00.000");
DateTime myDate;
if (!DateTime.TryParse(string.Format("2000-01-01 {0}", time), out myDate))
{
return 0;
}
return myDate.Subtract(date).Milliseconds;
}```
In future, try to give people ALL the information they need before they answer.

Just rereading your question - the time component is not a valid time component. General representation of time is Hours:Minutes:Seconds.Milliseconds; Note that there are only 2 colons in there and a period, not three colons.
v2
VJ Reddy 31-May-12 5:18am

sandeep nagabhairava 31-May-12 5:45am

hi pete.
actually
my requirement was like this totalAmount=(txtstring.text/minHours)*cost.

hear txtstring.text(string) n value like this 1:00:51:231(it was dif betwwen two times),
minHours(int64) is some X hours(ex:3 hrs),
Cost(int64) is some Y\$ (ex:4\$),
i wana calculate amount in int format.
Pete O'Hanlon 31-May-12 5:55am

So why didn't you state this? You cannot get a valid answer if you don't give us the real problem. This is your third attempt at the problem, which we could have given you an answer to straight off rather than you wasting our time.
Rahul Rajat Singh 31-May-12 6:07am

Pete O'Hanlon 31-May-12 6:53am

Thanks. I appreciate the thought.
Rate this:

## Solution 9

You don't need to convert the time to an integer for doing that, all you will be needing is hours.

```string [] timeDuration = txtstring.Text.Split(':');

if (Convert.ToInt32(timeDuration[3]) > 0)//Check if milliseconds greater than 0
{
//If greater than 0 then increment seconds.
timeDuration[2] = (Convert.ToInt32(timeDuration[2]) + 1).ToString();
}
if (Convert.ToInt32(timeDuration[2]) > 0)//Check if seconds greater than 0
{
//If greater than 0 then increment minutes.
timeDuration[1] = (Convert.ToInt32(timeDuration[1]) + 1).ToString();
}
if (Convert.ToInt32(timeDuration[1]) > 0)//Check if minutes greater than 0
{
//If greater than 0 then increment Hours.
timeDuration[0] = (Convert.ToInt32(timeDuration[0]) + 1).ToString();
}
Int32 Hours = Convert.ToInt32(timeDuration[0]);```

Finally you will have Hours as integer for getting the amount.

Hope it helps.
Rate this:

## Solution 1

sandeep nagabhairava 31-May-12 4:42am

it's not working, my string was like this txtstring.text having the value like this 1:00:51:231.
Rate this:

## Solution 10

I know this post has been answered already, but for those who are looking for benchmarks, he benchmarks several different ways to convert a string to an int:

http://cc.davelozinski.com/c-sharp/fastest-way-to-convert-a-string-to-an-int

What's interesting is the fastest way isn't using any of the native C# methods Convert.ToInt32(), int.TryParse(), or int.Parse().

Definitely worth a read for those that are curious.
v2

Top Experts
Last 24hrsThis month
 OriginalGriff 291 Karthik Bangalore 215 Sergey Alexandrovich Kryukov 195 phil.o 145 Peter Leow 144
 OriginalGriff 10,311 ppolymorphe 5,966 CHill60 5,867 Karthik Bangalore 5,566 Sergey Alexandrovich Kryukov 5,443