日期:2014-05-18 浏览次数:21317 次
class Program { static void Main() { string hexstr = "30"; string ascii = ((char)int.Parse(hexstr, System.Globalization.NumberStyles.HexNumber)).ToString(); System.Console.WriteLine(ascii); } }
------解决方案--------------------
string str = "30"; int sss=Convert.ToInt32(str,16);
------解决方案--------------------
给你一段完整的代码:
const string HexChs = "0123456789ABCDEF";
private static string str2hex(string s)
{
StringBuilder sb = new StringBuilder();
for (int i=0;i<s.Length; i++) {
if (i>0) sb.Append(' ');
sb.Append( ((UInt16)s[i]).ToString("X2"));
}
return sb.ToString();
}
private static string hex2str(string s)
{
string str = s.ToUpper();
StringBuilder sb = new StringBuilder();
for (int i=0; i<str.Length; i+=3){
int n1 = HexChs.IndexOf( str[i]);
if (n1==-1) throw new Exception("格式错");
if (i+1==str.Length) throw new Exception("格式错");
int n2 = HexChs.IndexOf( str[i+1]);
if (n2==-1) throw new Exception("格式错");
if (i+2< str.Length && str[i+2]!= ' ') throw new Exception("格式错");
char c = (char)((n1 << 4) + n2);
sb.Append(c);
}
return sb.ToString();
}
------解决方案--------------------
public static String toStringHex(String s) { byte[] baKeyword = new byte[s.length()/2]; for(int i = 0; i < baKeyword.length; i++) { try { baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16)); } catch(Exception e) { e.printStackTrace(); } } try { s = new String(baKeyword, "utf-8");// UTF-16le:Not } catch (Exception e1) { e1.printStackTrace(); } return s; }