Java Hex Calculate tohex(final byte[] bytes)

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

Description

tohex

License

Open Source License

Declaration

public static String tohex(final byte[] bytes) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2017 JCrypTool Team and Contributors
 *
 * All rights reserved. This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

public class Main {
    private static final int UNSIGNED_BYTE_MASK = 0xFF;

    public static String tohex(final byte[] bytes) {
        final StringBuilder sb = new StringBuilder(bytes.length * 2);
        for (int i = 0; i < bytes.length; i++) {
            sb.append(String.format("%02X", bytes[i] & UNSIGNED_BYTE_MASK));
        }//from w w  w.  ja  va2 s.  c  o m
        return sb.toString();
    }

    public static String tohex(long[] longs) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < longs.length; i++) {
            sb.append(String.format("0x%016X", longs[i]));
            if (i != longs.length - 1) {
                sb.append(", ");
                if (i % 4 == 3) {
                    sb.append(String.format("%n"));
                }
            }
        }

        sb.append(String.format("%n"));

        return sb.toString();
    }
}

Related

  1. toHex(final byte[] bytes)
  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, final int offset, final int count)
  7. toHex(final byte[] data)
  8. toHex(final byte[] data)
  9. toHex(final byte[] data)