The solution is actually really simple, instead of using all kinds of quirks to format the number in hexadecimal, you can dig a class in NumberFormatInfo .
The solution to your problem is as follows:
return string.Format("0x{0:X}", temp);
Although I would not have made an extension method for this use.