convert a number from base 10 to base 16 in csharp

To convert a number from base 10 to base 16 in C#, you can use the built-in ToString() method and specify the base as 16. Here's an example:

main.cs
int num = 1234;
string hex = num.ToString("X");
Console.WriteLine(hex);
72 chars
4 lines

In this example, we first declare the integer num with a value of 1234. We then convert it to a hex string by calling ToString() with the "X" format specifier, which specifies that we want to use hexadecimal (base 16). Finally, we print out the result using Console.WriteLine().

The output of this code would be:

main.cs
4D2
4 chars
2 lines

Which is the hexadecimal representation of the original number 1234.

gistlibby LogSnag