Java Byte Array Encode encodeHex(byte[] bytes)

Here you can find the source of encodeHex(byte[] bytes)

Description

encode Hex

License

Apache License

Declaration

public static String encodeHex(byte[] bytes) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
 * Copyright (c) 2011- kotemaru@kotemaru.org
 ******************************************************************************/

public class Main {
    private final static String UTF8 = "UTF-8";
    private final static char[] HEX_MAP = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D',
            'E', 'F' };

    public static String encodeHex(String str) {
        try {/*  w w w . j  a va  2  s.c o m*/
            return encodeHex(str.getBytes(UTF8));
        } catch (java.io.UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String encodeHex(byte[] bytes) {
        char[] buff = new char[bytes.length * 2];
        for (int i = 0; i < bytes.length; i++) {
            int h0 = (bytes[i] >> 4) & 0x0f;
            int h1 = bytes[i] & 0x0f;
            buff[i * 2] = HEX_MAP[h0];
            buff[i * 2 + 1] = HEX_MAP[h1];
        }
        return new String(buff);
    }
}

Related

  1. encodeBitString(byte[] in, OutputStream os)
  2. encodeByteArray(DataOutputStream out, byte[] bytes)
  3. encodeBytes(byte[] source, int off, int len)
  4. encodeCompositeAlways(List bytesList)
  5. encodeDataWithCharsetInMetaTag(byte[] data)
  6. encodeHex(byte[] bytes)