Hex function

  • Thread starter Thread starter Joshua Ellul
  • Start date Start date
J

Joshua Ellul

Hi,

Can anyone tell me if there is a function in C# that will return a value in
Hex format? Similiar to the Hex function of vb.

Regards,

Josh
 
you could use the ToString function:

int myVal = 171;

// this will give you 0xAB
str myValHex = myVal.ToString("X");

hope that helps..
Imran.
 
Try

/// <summary>
/// Converts specified string to hexa string.
/// </summary>
/// <param name="text"></param>
/// <returns></returns>
public static string Hex(string text)
{
return
BitConverter.ToString(Encoding.Default.GetBytes(text)).ToLower().Replace("-","");
}
 
This may help:

int i = 2987382;
string hexval = i.ToString("X8"); // X indicated uppercase hex, 8 = show
8 places

For more info, see IntXX.ToString and the NumberFormatInfo class in the
online help.

HTH,
-sb
 
Back
Top