Java Hex Calculate 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;
/*/* ww  w. ja  va2  s .  c om*/
 * Copyright 2015-2016 the original author or authors.
 *
 * 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. toHex(final byte[] ba)
  2. toHex(final byte[] bytes)
  3. toHex(final byte[] bytes)
  4. toHex(final byte[] bytes)
  5. toHex(final byte[] bytes)
  6. toHex(final byte[] bytes)
  7. toHex(final byte[] bytes)
  8. tohex(final byte[] bytes)
  9. toHex(final byte[] bytes, final int offset, final int count)