java 十六进制字符串与字符串之间的转换

原文地址:http://blog.csdn.net/chumeng411/article/details/6543002

 

public static String toHexString(int i)以十六进制的无符号整数形式返回一个整数参数的字符串表示形式。
如果参数为负,那么无符号整数值为参数加上 232;否则等于该参数。将该值转换为十六进制(基数 16)的无前导 0 的 ASCII
数字字符串。如果无符号数的大小值为零,则用一个零字符 '0' ('/u0030')
表示它;否则,无符号数大小的表示形式中的第一个字符将不是零字符。用以下字符作为十六进制数字:
0123456789abcdef
这些字符的范围是从 '/u0030' 到 '/u0039' 和从 '/u0061' 到 '/u0066'。如果希望得到大写字母,可以在结果上调用
String.toUpperCase() 方法:
Integer.toHexString(n).toUpperCase()
参数:
i - 要转换成字符串的整数。
返回:
用十六进制(基数 16)参数表示的无符号整数值的字符串表示形式。

[java]   view plain copy
  1. //转化字符串为十六进制编码  
  2. public static String toHexString(String s)  
  3.    String str "" 
  4.    for (int 0s.length(); i++)  
  5.     int ch (ints.charAt(i);  
  6.     String s4 Integer.toHexString(ch);  
  7.     str str s4;  
  8.     
  9.    return str;  
  10.  
  11. // 转化十六进制编码为字符串  
  12. public static String toStringHex1(String s)  
  13.    byte[] baKeyword new byte[s.length() 2];  
  14.    for (int 0baKeyword.length; i++)  
  15.     try  
  16.      baKeyword[i] (byte(0xff Integer.parseInt(s.substring(  
  17.        22 2), 16));  
  18.     catch (Exception e)  
  19.      e.printStackTrace();  
  20.      
  21.     
  22.    try  
  23.     new String(baKeyword, "utf-8");// UTF-16le:Not  
  24.    catch (Exception e1)  
  25.     e1.printStackTrace();  
  26.     
  27.    return s;  
  28.  
  29. // 转化十六进制编码为字符串  
  30. public static String toStringHex2(String s)  
  31.    byte[] baKeyword new byte[s.length() 2];  
  32.    for (int 0baKeyword.length; i++)  
  33.     try  
  34.      baKeyword[i] (byte(0xff Integer.parseInt(s.substring(  
  35.        22 2), 16));  
  36.     catch (Exception e)  
  37.      e.printStackTrace();  
  38.      
  39.     
  40.    try  
  41.     new String(baKeyword, "utf-8");// UTF-16le:Not  
  42.    catch (Exception e1)  
  43.     e1.printStackTrace();  
  44.     
  45.    return s;  
  46.  
  47. public static void main(String[] args)  
  48.    System.out.println(encode("中文"));  
  49.    System.out.println(decode(encode("中文")));  
  50.  
  51.   
  52. private static String hexString "0123456789ABCDEF" 
  53.   
  54. public static String encode(String str)  
  55.    // 根据默认编码获取字节数组  
  56.    byte[] bytes str.getBytes();  
  57.    StringBuilder sb new StringBuilder(bytes.length 2);  
  58.    // 将字节数组中每个字节拆解成2位16进制整数  
  59.    for (int 0bytes.length; i++)  
  60.     sb.append(hexString.charAt((bytes[i] 0xf0>> 4));  
  61.     sb.append(hexString.charAt((bytes[i] 0x0f>> 0));  
  62.     
  63.    return sb.toString();  
  64.  
  65.   
  66. public static String decode(String bytes)  
  67.    ByteArrayOutputStream baos new ByteArrayOutputStream(  
  68.      bytes.length() 2);  
  69.    // 将每2位16进制整数组装成一个字节  
  70.    for (int 0bytes.length(); += 2 
  71.     baos.write((hexString.indexOf(bytes.charAt(i)) << 4 hexString  
  72.       .indexOf(bytes.charAt(i 1))));  
  73.    return new String(baos.toByteArray());  
  74.  
  75. // 第二种方法:  
  76. // 将指定byte数组以16进制的形式打印到控制台  
  77. // 复制代码 代码如下:  
  78. public class Util  
  79.    public Util()  
  80.     
  81.      
  82.    public static void printHexString(String hint, byte[] b)  
  83.     System.out.print(hint);  
  84.     for (int 0b.length; i++)  
  85.      String hex Integer.toHexString(b[i] 0xFF);  
  86.      if (hex.length() == 1 
  87.       hex '0' hex;  
  88.       
  89.      System.out.print(hex.toUpperCase() ");  
  90.      
  91.     System.out.println("");  
  92.     
  93.      
  94.    public static String Bytes2HexString(byte[] b)  
  95.     String ret "" 
  96.     for (int 0b.length; i++)  
  97.      String hex Integer.toHexString(b[i] 0xFF);  
  98.      if (hex.length() == 1 
  99.       hex '0' hex;  
  100.       
  101.      ret += hex.toUpperCase();  
  102.      
  103.     return ret;  
  104.     
  105.      
  106.    public static byte uniteBytes(byte src0, byte src1)  
  107.     byte _b0 Byte.decode("0x" new String(new byte[] src0 }))  
  108.       .byteValue();  
  109.     _b0 (byte(_b0 << 4);  
  110.     byte _b1 Byte.decode("0x" new String(new byte[] src1 }))  
  111.       .byteValue();  
  112.     byte ret (byte(_b0 _b1);  
  113.     return ret;  
  114.     
  115.      
  116.    public static byte[] HexString2Bytes(String src)  
  117.     byte[] ret new byte[8];  
  118.     byte[] tmp src.getBytes();  
  119.     for (int 08i++)  
  120.      ret[i] uniteBytes(tmp[i 2], tmp[i 2 1]);  
  121.      
  122.     return ret;  
  123.     
  124. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值