Hex format in C# [duplicate]

2020-05-10 09:49发布

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条回答
The star\"
2楼-- · 2020-05-10 10:07

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).

查看更多
登录 后发表回答