问题
Possible Duplicate: C# - Convert a string of hex values to hex
I converted the following code from Visual Basic to C#. But how do I know how I can use Hex in C#?
private string ConvertStringToHex(string sText)
{
int lCount;
string sHex;
string sResult;
for (lCount = 1; (lCount <= sText.Length); lCount++)
{
sHex = Hex(Convert.ToInt32(sText.Substring((lCount - 1), 1)));
if ((sHex.Length == 1))
{
sHex = ("0" + sHex);
}
sResult = (sResult + sHex);
}
return sResult;
}
回答1:
If you simply want to convert to hexadecimal, then you can probably do it like:
int val = Convert.ToInt32(sText);
string hexval = val.ToString("X");
Hex()
is a function for returning a string representing the hexadecimal value of a number avilable in VB/VB.NET.
See Hex Function (Visual Basic) (MSDN).
来源:https://stackoverflow.com/questions/10662552/hex-format-in-c-sharp