Convert from string ascii to string Hex

前端 未结 7 1192
既然无缘
既然无缘 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 10:49

    Convert to byte array and then to hex

            string data = "1234";
    
            // Convert to byte array
            byte[] retval = System.Text.Encoding.ASCII.GetBytes(data);
    
            // Convert to hex and add "0x"
            data =  "0x" + BitConverter.ToString(retval).Replace("-", " 0x");
    
            System.Diagnostics.Debug.WriteLine(data);
    

提交回复
热议问题