首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

16进制字符串怎么转换为对应的ASCII码

2012-07-31 
16进制字符串如何转换为对应的ASCII码?16进制的字符串如何转换成对应的ASCII码?CString hexstr 30str

16进制字符串如何转换为对应的ASCII码?
16进制的字符串如何转换成对应的ASCII码?

CString hexstr = "30";

string ascii = xxxx(hexstr);//有没有.NET框架的系统函数可以转换的呢?谢谢!不会自己建个内存表来查找吧?


如何得到对应的ASCII码?


[解决办法]

C# code
class Program{  static void Main()  {    string hexstr = "30";    string ascii = ((char)int.Parse(hexstr, System.Globalization.NumberStyles.HexNumber)).ToString();      System.Console.WriteLine(ascii);  }}
[解决办法]
C# code
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();
}

[解决办法]
探讨
反过来怎么算

知道 ASCII 0 怎么转换到 16进制30去?

[解决办法]
Java code
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;     } 

热点排行