Java Byte to Hex String byteToHexString(byte src)

Here you can find the source of byteToHexString(byte src)

Description

byte To Hex String

License

Apache License

Declaration

public static String byteToHexString(byte src) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static String byteToHexString(byte src) {
        StringBuilder stringBuilder = new StringBuilder("");
        int v = src & 0xFF;
        String hv = Integer.toHexString(v);
        if (hv.length() < 2) {
            stringBuilder.append(0);/*w  w w  . j  a  va 2 s  .c o  m*/
        }
        stringBuilder.append(hv);
        return stringBuilder.toString();
    }

    public static String toHexString(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;
    }
}

Related

  1. byteToHexString(byte data)
  2. byteToHexString(byte data)
  3. byteToHexString(byte ib)
  4. byteToHexString(byte in)
  5. byteToHexString(byte num)
  6. byteToHexString(byte value)
  7. byteToHexString(byte value)
  8. byteToHexString(byte[] b)
  9. byteToHexString(byte[] b)