16 與ascii相互轉化

謝謝!
//   轉化字符串爲十六進制編碼
public   static   String   toHexString(String   s)      
{      
String   str= " ";      
for   (int   i=0;i <s.length();i++)      
{      
int   ch   =   (int)s.charAt(i);      
String   s4   =   Integer.toHexString(ch);      
str   =   str   +   s4;  
}      
return   str;      
}  

我現在需要的是把十六進制轉換爲字符串,請幫忙!
 

 

String   str= "中 ";
char   c=(char)str.charAt(0);
System.out.println(c+ "轉換爲16進製爲: "+Integer.toString(c,16));
System.out.println( "16進制轉換爲字符串爲: "+(char)Integer.valueOf(Integer.toString(c,   16),16).intValue());
#3樓 得分:0回覆於:2007-05-15 10:37:15
已經解決,放上來跟大家共享哈,謝謝大家的支持
//   轉化十六進制編碼爲字符串
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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章