Android Byte Array to Hex Convert toHex(final byte[] bytes)

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

Description

to Hex

License

Apache License

Declaration

public static String toHex(final byte[] bytes) 

Method Source Code

//package com.java2s;
/*//from w  ww.  j a  va  2s.c  o m
 * Copyright 2004-2011 the Seasar Foundation and the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

public class Main {

    public static String toHex(final byte[] bytes) {
        if (bytes == null) {
            return "";
        }
        StringBuffer sb = new StringBuffer(bytes.length * 2);
        for (int i = 0; i < bytes.length; ++i) {
            appendHex(sb, bytes[i]);
        }
        return sb.toString();
    }

    public static String toHex(final int i) {
        StringBuffer buf = new StringBuffer();
        appendHex(buf, i);
        return buf.toString();
    }

    public static void appendHex(final StringBuffer buf, final byte i) {
        buf.append(Character.forDigit((i & 0xf0) >> 4, 16));
        buf.append(Character.forDigit((i & 0x0f), 16));
    }

    public static void appendHex(final StringBuffer buf, final int i) {
        buf.append(Integer.toHexString((i >> 24) & 0xff));
        buf.append(Integer.toHexString((i >> 16) & 0xff));
        buf.append(Integer.toHexString((i >> 8) & 0xff));
        buf.append(Integer.toHexString(i & 0xff));
    }
}

Related

  1. ConvertHexString(byte[] b)
  2. getHexStringOfByte(byte[] bytes)
  3. bytesToHexString(byte[] bytes)
  4. bytesToHexString(byte[] bytes)
  5. convertToHex(byte[] data)
  6. byteArrayToHexString(byte[] b)
  7. toHexString(byte abyte0[], int beginIndex, int endIndex, boolean spaceFlag)
  8. toHexString(byte abyte0[], int beginIndex, int endIndex)
  9. toHexString(byte abyte0[], boolean spaceFlag)