HexEncode.java 876 B

1234567891011121314151617181920212223242526272829
  1. package com.yihu.ehr.util.encode;
  2. /**
  3. * @created Created by Air on 2015/6/2.
  4. */
  5. public class HexEncode {
  6. static public String toHexString(byte[] bytes) {
  7. StringBuilder stringBuffer = new StringBuilder();
  8. for (byte aByte : bytes) {
  9. if (Integer.toHexString(0xFF & aByte).length() == 1) {
  10. stringBuffer.append("0").append(Integer.toHexString(0xFF & aByte));
  11. } else {
  12. stringBuffer.append(Integer.toHexString(0xFF & aByte));
  13. }
  14. }
  15. return stringBuffer.toString();
  16. }
  17. static public byte[] toBytes(String hexString) {
  18. byte[] bytes;
  19. bytes = new byte[hexString.length() / 2];
  20. for (int i = 0; i < bytes.length; i++) {
  21. bytes[i] = (byte) Integer.parseInt(hexString.substring(2 * i, 2 * i + 2), 16);
  22. }
  23. return bytes;
  24. }
  25. }