Turn byte into two-digit hexadecimal number just using ToString?

Timwi picture Timwi · Mar 24, 2011 · Viewed 52.4k times · Source

I can turn a byte into a hexadecimal number like this:

myByte.ToString("X")

but it will have only one digit if it is less than 0x10. I need it with a leading zero. Is there a format string that makes it possible to do this in a single call to ToString?

Answer

Roman Starkov picture Roman Starkov · Mar 24, 2011

myByte.ToString("X2") I believe.