This is the improved solution, instead of the one which was auto-removed with your next question, due to some abuse reports:
First of all, the notion of decimal or hexadecimal digits is inapplicable to numeric objects, which are always "binary". Therefore, you can represent the numeric value to string and modify it. This is not the best solution in terms of performance, but it is most practical and easy to maintain. It can be something like
static class Something {
internal static string LastDigitsOfHexNumber(int value, byte numberOfDigits) {
string result = string.Format(hexFormat, value);
return result.Remove(0, maxNumberOfDigitsInHexInt - numberOfDigits);
}
const int maxNumberOfDigitsInHexInt = sizeof(int) * 2;
static readonly string hexFormat =
string.Format("{{0:X{0}}}", maxNumberOfDigitsInHexInt);
}
Note the constant and static readonly members. This is the way to avoid hard-coding of
immediate constants 8 and "{0:X8}" and, at the same time, avoiding recalculation of these values on each call. Such techniques look more sophisticated, but they greatly improve maintenance.
Now, I would advise you to avoid both re-posting and self-answering, to avoid abuse reports. Your removed "answer" wasn't correct and wasn't even worse. More essentially, people generally are not really interested to see how you failed to solve some trivial problem and later solved it. It only adds garbage to the site.
Thank you for your understanding.
—SA