It is easy to see that G is between A and Z, but if I have a user enter a range of serial numbers from 56AAA7105A25
to 56AAA71064D6
, how can I determine if another serial number or range falls between the two serial numbers specified before. In order for me to know if it falls between them, wouldn't I need to know how it rolls over. The problem I am facing is that according to code (c# in this case), one value would be considered greater than and less than the two values I s开发者_Python百科pecified, but according to the user, the value they enter is actually outside the range. How do I handle scenarios like this?
So your serial numbers can be ordered - you need to find out what algorithm the manufacturer uses. The next thing to do is to use that algorithm to define an IComparer<string>
that will compare two serial numbers to encapsulate that logic - you would have something like so:
public class SerialComparer : IComparer<T>
{
public int Compare(string x, string y)
{
// If x == y:
// return 0
// If x > y:
// return 1
// Else:
// return -1
}
}
public static void Main(string[] args)
{
var x = "56AAA7105A25";
var y = "56AAA71064D6";
var comparer = new SerialComparer();
if (comparer.Compare(x, y) > 0)
{
Console.WriteLine("{0} is greater than {1}", x, y);
}
else
{
Console.WriteLine("{0} is not greater than {1}", x, y);
}
}
If you don't know the algorithm for determining ordering then you need to find it. You can try to guess but this is going to probably lead to pain in the long run because the algorithm need not be simple.
Somebody must be able to tell you the answer to this? Your user sounds like they have some idea (enough to tell you you are wrong at least), whoever specced the code you are writing should do (if appropriate) or perhaps if the serial numbers are generated by another app you can find out from the developers of that app (or its documentation).
Once that algorithm is known it can be coded and then the next step is obviously pretty trivial. The key is trying to define the comparison which we can't help with because we don't know what it is either. :)
Assuming those are hexadecimal numbers you should be able to convert them to a long(they're too big for a 32 bit int at 48 bits)
long.Parse("0x" + serialnumber);
Alternately, if you do know what the valid numbers are, and they aren't in a natural order, you can make a custom class, override the <
and >
operators, and throw an exception if a known invalid value is entered.
you can convert the serial numbers to an int and then check if one is between the others. If the serial numbers are just hex numbers, this is not at all difficult, if they consist of the entire alphabet, you just have to build a function that converts the number with a given base (0-9 + A-Z would be 25, I guess) to an int. Just see A-Z as digits with value 10-25.
Yes. You need to know how the serial numbers are defined. If you don't know yourself how two serial numbers compare then your code won't be able to get it right either.
Just go through the number from left to right (don't forget to pad all numbers with zeros to the same length), and compare the two digits/letters on this position. If they differ, you can break the loop.
What about something like this?
class Program
{
static void Main(string[] args)
{
SerialNumber first = new SerialNumber("56AAA7105A25");
SerialNumber second = new SerialNumber("57AAA71064C6");
SerialNumber third = new SerialNumber("58AAA71064D6");
Console.WriteLine(first.CompareTo(second));
Console.WriteLine(second.CompareTo(third));
Console.ReadLine();
}
}
struct SerialNumber : IComparable<SerialNumber>
{
public string Serial { get; set; }
public SerialNumber(string serial) : this()
{
this.Serial = serial;
}
public int CompareTo(SerialNumber other)
{
int compareTo = 0;
using (CharEnumerator enum1 = Serial.GetEnumerator())
using (CharEnumerator enum2 = other.Serial.GetEnumerator())
{
while (enum1.MoveNext() && enum2.MoveNext())
{
if (enum1.Current != enum2.Current)
{
compareTo = enum1.Current.CompareTo(enum2.Current);
break;
}
}
}
return compareTo;
}
}
精彩评论