Convert from string ascii to string Hex

前端 未结 7 1066
既然无缘
既然无缘 2021-02-07 10:31

Suppose I have this string

string str = \"1234\"

I need a function that convert this string to this string:

\"0x31 0x32 0x33          


        
相关标签:
7条回答
  • 2021-02-07 11:07
    static void Main(string[] args)
    {
        string str = "1234";
        char[] array = str.ToCharArray();
        string final = "";
        foreach (var i in array)
        {
            string hex = String.Format("{0:X}", Convert.ToInt32(i));
            final += hex.Insert(0, "0X") + " ";       
        }
        final = final.TrimEnd();
        Console.WriteLine(final);
    }
    

    Output will be;

    0X31 0X32 0X33 0X34
    

    Here is a DEMO.

    0 讨论(0)
提交回复
热议问题