Convert from string ascii to string Hex

前端 未结 7 1085
既然无缘
既然无缘 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:51

    string str = "1234";
    char[] charValues = str.ToCharArray();
    string hexOutput="";
    foreach (char _eachChar in charValues )
    {
        // Get the integral value of the character.
        int value = Convert.ToInt32(_eachChar);
        // Convert the decimal value to a hexadecimal value in string form.
        hexOutput += String.Format("{0:X}", value);
        // to make output as your eg 
        //  hexOutput +=" "+ String.Format("{0:X}", value);
    
    }
    
        //here is the HEX hexOutput 
        //use hexOutput 
    

提交回复
热议问题