1. import java.io.ByteArrayOutputStream;
  2.  
  3. public class HexUtil {
  4.  
  5. /**
  6. * @param args
  7. */
  8. public static void main(String[] args) {
  9.  
  10. String str = "12345678";
  11.  
  12. String hexStr = encode(str);
  13.  
  14. System.out.println("字符串->十六进制:" + hexStr);
  15.  
  16. System.out.println("十六进制->字符串:" + decode(hexStr));
  17.  
  18. System.out.println("十六进制->byte数组:" + HexString2Bytes(hexStr));
  19.  
  20. }
  21.  
  22. public static String stringToHexString(String strPart) {
  23. String hexString = "";
  24. for (int i = 0; i < strPart.length(); i++) {
  25. int ch = (int) strPart.charAt(i);
  26. String strHex = Integer.toHexString(ch);
  27. hexString = hexString + strHex;
  28. }
  29. return hexString;
  30. }
  31.  
  32. private static String hexString = "0123456789ABCDEF";
  33.  
  34. /*
  35. * 将字符串编码成16进制数字,适用于所有字符(包括中文)
  36. */
  37. public static String encode(String str) {
  38. // 根据默认编码获取字节数组
  39. byte[] bytes = str.getBytes();
  40. StringBuilder sb = new StringBuilder(bytes.length * 2);
  41. // 将字节数组中每个字节拆解成2位16进制整数
  42. for (int i = 0; i < bytes.length; i++) {
  43. sb.append(hexString.charAt((bytes[i] & 0xf0) >> 4));
  44. sb.append(hexString.charAt((bytes[i] & 0x0f) >> 0));
  45. }
  46. return sb.toString();
  47. }
  48.  
  49. /*
  50. * 将16进制数字解码成字符串,适用于所有字符(包括中文)
  51. */
  52. public static String decode(String bytes) {
  53. ByteArrayOutputStream baos = new ByteArrayOutputStream(
  54. bytes.length() / 2);
  55. // 将每2位16进制整数组装成一个字节
  56. for (int i = 0; i < bytes.length(); i += 2)
  57. baos.write((hexString.indexOf(bytes.charAt(i)) << 4 | hexString
  58. .indexOf(bytes.charAt(i + 1))));
  59. return new String(baos.toByteArray());
  60. }
  61.  
  62. private static byte uniteBytes(byte src0, byte src1) {
  63. byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
  64. .byteValue();
  65. _b0 = (byte) (_b0 << 4);
  66. byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
  67. .byteValue();
  68. byte ret = (byte) (_b0 | _b1);
  69. return ret;
  70. }
  71.  
  72. public static byte[] HexString2Bytes(String src) {
  73. byte[] ret = new byte[6];
  74. byte[] tmp = src.getBytes();
  75. for (int i = 0; i < 6; ++i) {
  76. ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
  77. }
  78. return ret;
  79. }
  80. }

字符串、十六进制、byte数组互转的更多相关文章

  1. Golang十六进制字符串和byte数组互转

    Golang十六进制字符串和byte数组互转 需求 Golang十六进制字符串和byte数组互相转换,使用"encoding/hex"包 实现Demo package main i ...

  2. 二进制样式的字符串与byte数组互转函数示例

    开发时用到的方法,记录下: /// <summary> /// 测试方法 /// </summary> private void TestFun() { Response.Wr ...

  3. 字符串与byte数组转换

    string weclome=""; byte[] data = new byte[1024]; //字符串转byte数组 data = Encoding.ASCII.GetByt ...

  4. java中 16进制字符串 与普通字符串 与 byte数组 之间的转化

    方法依赖commons-codec包  maven的引入方式如下 <dependency> <groupId>commons-codec</groupId> < ...

  5. 16进制字符串和byte数组进行相互转换\将10进制转换为任意进制

    16进制字符串和byte数组进行相互转换 简介 1个byte对应8个bit,16进制使用4个bit,所以一个byte转成16进制,占用两位. JAVA代码 private static final c ...

  6. 字符串和byte数组的相互转化

    关于byte[]数组转十六进制字符串: public static String getHexString(byte[] b) throws Exception { String result = & ...

  7. int跟byte[]数组互转的方法,整数 + 浮点型

    整数: int转byte数组 public static byte[] intToBytes2(int n){ ]; ;i < ;i++) { b[i]=(-i*)); } return b; ...

  8. Java中字符串和byte数组之间的相互转换

    1.将字符转换成byte数组 String str = "罗长"; byte[] sb = str.getBytes(); 2.将byte数组转换成字符 byte[] b={(by ...

  9. 图片和byte[]数组互转

    一.图片转成byte[]数组. import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io ...

随机推荐

  1. Laravel_1 安装

    1>http://www.golaravel.com/post/install-and-run-laravel-5-x-on-windows/ 2>http://www.golaravel ...

  2. 实习之vim基本学习

    最近实习学到了写vim的基本用法,记录一下 批量注释 ctrl+v进入列模式,按“I”进入插入模式,按// #等在每行开头插入注释,esc 批量去除注释 ctrl + v 进入列模式,按“x”即可. ...

  3. Windows phone 之Interaction.Triggers的使用

    两个步骤:1.添加以下两个程序集System.Windows.InteractivityMicrosoft.Expression.Interactions 2.添加xmlns:i="clr- ...

  4. Bad owner or permissions on .ssh/config

    Bad owner or permissions on $HOME/.ssh/config The ssh with RHEL 4 is a lot more anal about security ...

  5. js 中特殊形势的函数-匿名函数的应用

    javascript中的匿名函数,那什么叫做匿名函数? 匿名函数就是没有函数名称:演示代码: <script> function(x,y){ return x+y //这个就是一个匿名函数 ...

  6. SSO单点登录的实现原理

    单点登录在现在的系统架构中广泛存在,他将多个子系统的认证体系打通,实现了一个入口多处使用,而在架构单点登录时,也会遇到一些小问题,在不同的应用环境中可以采用不同的单点登录实现方案来满足需求.我将以我所 ...

  7. HMM模型

    通过前几时断续的学习,发现自己对HMM模型的了解还只停留在皮毛,导致在学习CRF模型并将其与最大熵模型.HMM.MEMM做比较时感觉很吃力,所以又花了两天时间使劲看了遍HMM,发现了解得确实深刻了很多 ...

  8. OC语言-02面向对象的三大特性

    01封装 #import <Foundation/Foundation.h> @interface Student : NSObject { //@public 成员变量尽量不使用 int ...

  9. Word 2016 test

    Word 2016 test    

  10. Maven入门详解以及Eclisp的集成

    1.首先要安装Maven到操作系统上 Maven的下载页面:http://maven.apache.org/download.html Maven跟Tomcat很像,下载下来后直接解压在指定的目录就安 ...