Hex format in C# [duplicate]

回眸只為那壹抹淺笑 提交于 2019-12-02 23:49:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!