tags:

views:

759

answers:

9

I can get it as a string:

Assembly.GetExecutingAssembly().GetName().Version()

How can I convert this number to an int?


Example input:

8.0.0.1

Expected output:

8001

+1  A: 

You could string.Split('.'), iterate and int.parse()

Jason Watts
+1 for splitting. This'll let you format the end result however you want, since you seem particular on how the final integer should look.
Will Eddins
A: 

They are numbers, so you could just convert them to strings and stack them after each other:

Version v = Assembly.GetExecutingAssembly().GetName().Version;
string compacted = string.Format("{0}{1}{2}{3}", v.Major, v.Minor, v.Build, v.Revision);
int compactInt = int.Parse(compacted);

This of course only works well as long as each component is less than ten. After that you can't determine what the actual version is. The string "12345" can be 1.23.4.5 or 12.3.4.5 or 1.2.34.5 or...

If you know that they are all below ten, you could do it numerically:

Version v = Assembly.GetExecutingAssembly().GetName().Version;
int compactInt = v.Major * 1000 + v.Minor * 100 + v.Build * 10 + v.Revision;

If you don't know that, it gets a bit more complicated, but you can still do it without creating a string and parse it. It would be a bit faster, but it's 10 - 20 lines of code.

Guffa
+1  A: 

The following 'works':

string version = "8.0.0.1";
int versionNumber = int.Parse(version.Replace(".", ""));

However, there would be no difference between version 8.0.11.0 and 8.0.1.10!

samjudson
thanks that works, I just realized that I need in terms of minor major... I basically need if it s 8.0.1.0 be converted to 801000
something like that? string version = "9.0.1.0";int versionNumber = int.Parse(version+"0.0".Replace(".", ""));
A: 

I would just do this:

Integer.Parse(Assembly.GetExecutingAssembly().GetName().Version().ToString().Replace(".", ""))

I stand Corrected.

Avitus
You can't cast a string to an int. You have to parse it. Int.Parse or Convert.ToInt32 can do that.
Guffa
You can't just strip out the "." characters because for example (as AdamRalph and probably others pointed out) stripping the periods out of version 8.1.11.0 and version 8.11.1.0 will both result in the same integer value: 81110
Adam Porad
A: 

You could also run into the situation where an assembly has a version of 0.0.1.1 and then the version looks like: 11. Definitely not desired.

jasonh
A: 

Try this:

Version v = Assembly.GetExecutingAssembly().GetName().Version;
long s = long.Parse(v.Major.ToString("0000") + v.MajorRevision.ToString("0000") + v.Minor.ToString("0000") + v.MinorRevision.ToString("0000"));
RedFilter
A: 

What is the problem you're trying to solve? Are you checking to see if the running version of some library is greater than or equal to some known bug-free version? If so, the right thing to do is not to treat a .-delimited string of numbers as a single number, but to compare them individually to your target.

DDaviesBrackett
In that case you can just compare the Version objects. The comparison operators are overloaded to handle Version objects.
Guffa
A: 

Rather than that approach, and assuming that each of your version string elements is less than 255, you could just take each element as a byte in a 32 bit unsigned integer. This would not be very human readable, but would ensure that if V1 < V2 in version comparison then f(V1) < f(V2) for the integer conversion f.

Adam Wright
The Build is 4 digits in most cases.
VVS
In which case, we could pull a couple of bits from major/minor to extend the build range to 2^14 odd.
Adam Wright
+4  A: 

If you really have to do this conversion, you should at least reserve an adequate amount of digits for every version part.

Two digits for Major, Minor and Revision and four for Build:

var version = Assembly.GetExecutingAssembly().GetName().Version();

Debug.Assert(version.Major >= 0 && version.Major < 100)
Debug.Assert(version.Minor >= 0 && version.Minor < 100)
Debug.Assert(version.Build >= 0 && version.Build < 10000)
Debug.Assert(version.Revision >= 0 && version.Revision < 100)

long longVesion = version.Major * 100000000L + 
                  version.Minor * 1000000L + 
                  version.Build * 100L + 
                  version.Revision;

int intVersion  = (int) longVersion;

Debug.Assert((long)intVersion == longVersion)

Note that this method will still fail for some exotic versions!

Don't even think about version 21.47.4836.48 :)

EDIT: Added assertions.

VVS
+1 for using Major/Minor/Build/Revision properties rather than parsing the version string. As you point out it can fail (including overflowing) for large numbers.
Joe