string HexFromID(int ID)
{
return ID.ToString("X");
}
int IDFromHex(string HexID)
{
return int.Parse(HexID, System.Globalization.NumberStyles.HexNumber);
}
I really question the value of this, though. You're stated goal is to make the value shorter, which it will, but that isn't a goal in itself. You really mean either make it easier to remember or easier to type.
If you mean easier to remember, then you're taking a step backwards. We know it's still the same size, just encoded differently. But your users won't know that the letters are restricted to 'A-F', and so the ID will occupy the same conceptual space for them as if the letter 'A-Z' were allowed. So instead of being like memorizing a telephone number, it's more like memorizing a GUID (of equivalent length).
If you mean typing, instead of being able to use the keypad the user now must use the main part of the keyboard. It's likely to be more difficult to type, because it won't be a word their fingers recognize.
A much better option is to actually let them pick a real username.
Integer
shortening implementation? If the only goal is to make the user ID as short as possible, I'd be interested to know if there is any other apparent reason why you specifically require hexadecimal conversion - unless I missed it of course. Is it clear and known (if so required) that user IDs are in actual fact a hexadecimal representation of the real value? – WynandB