Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 Eclipse RDF4J contributors, Aduna, and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Distribution License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *******************************************************************************/

public class Main {
    /**
     * Returns the hexadecimal value of the supplied byte array. The resulting string always uses two
     * hexadecimals per byte. As a result, the length of the resulting string is guaranteed to be twice the
     * length of the supplied byte array.
     */
    public static String toHexString(byte[] array) {
        StringBuilder sb = new StringBuilder(2 * array.length);

        for (int i = 0; i < array.length; i++) {
            String hex = Integer.toHexString(array[i] & 0xff);

            if (hex.length() == 1) {
                sb.append('0');
            }

            sb.append(hex);
        }

        return sb.toString();
    }
}