Convert from string ascii to string Hex

2020-06-01 07:21发布

Suppose I have this string

string str = "1234"

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

"0x31 0x32 0x33 0x34"  

I searched online and found a lot of similar things, but not an answer to this question.

7条回答
The star\"
2楼-- · 2020-06-01 08:10
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.

查看更多
登录 后发表回答