How to determine who is at a higher version?

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
I am trying to find a way to compare two versions that are stored in a text file, specifically I extract them both (as strings) and compare using standard operations (<, >, =<, =>, ==) - most of the time this works but I've just encountered a case that outright fails.

For example:
1 < 5
2a > 1b
75a > 54
92c > 92a

These all seem to work fine ... but then I just got this case
113 < 90A
(which is absolutly wrong, 113 is greater then 90A not less then)

So, I guess using the standard operators on my strings was a pretty bad idea - now I need to come up with an alternative.
The version is always NUMERICAL followed potentially by ALPHA characters (90, 113a, 113A, 75b, 22, etc...), no other format is acceptable.

I was thinking of extracting the numerical numbers and comparing them (can I use standard string operators for this?), and then if they are equal extract the ALPHA characters and compare them ...

a) seems like a pain, is this really the best/right way to go about it?
b) I've got really not much of a clue how to implement this checking function, is there something in C++ that allows you to easily split the string into both components and then somehow compare them?

Any help, hints, or ideas would be much appreciated...
Thanks,
 
These all seem to work fine ... but then I just got this case
113 < 90A
(which is absolutly wrong, 113 is greater then 90A not less then)

No, not quite
90A equals 2314.
Since the compiler sees a character behind a number, it will probably solve it as a HEX value,
90A equals 2314 in decimal numbers, which is greater then 113, so your computer is correct.
As with the other values you showed this seems the case:
1 < 5 .. 1 is less then 5
2a > 1b .. 42 is greater as 27
75a > 54 .. 1882 is greater as 54
92c > 92a .. 2348 is greater as 2346

~ DP

If you use a-f as a suffix the compiler will try to convert it to a heximal number (except .[nr]f, which will be a float (such as 12.0f) and 12f will be converted to its hex value (303 decimal))

Normal numbers are standard interpreted as decimal integer
 
Last edited:
Maybe your old comparison algorithm handled the strings as hex numbers:
113 hex = 275 decimal
90A hex = 2314 decimal


You could do the following before comparison:
1) Extract the numbers and convert them to integer variables.
2) Extract the characters and convert them to corresponding ascii numbers (integers). There is a function for this - In VB it's "Char()", but I'm not sure what it is in C#. Anyway the resulting numbers follow the order of the alphabets.
 
I'm pretty sure that the > and < operators compare the values of character codes, similar to how these operators would be expected to work with the Char data type. For example...
Visual Basic:
        Console.WriteLine("113" < "90")
The output of that code is "TRUE."

It shouldn't be unreasonably difficult to write code that extracts the different parts of the version string. Then you should use Integer.Parse to convert the strings to integers so you can use > and <.
 
Back
Top