Encodes an array of plain bytes into a hex encoded string - Java Internationalization

Java examples for Internationalization:Charset

Description

Encodes an array of plain bytes into a hex encoded string

Demo Code

/*//from  w ww .  j  av  a2 s. c  o m
 * Copyright (c) 2010 Matthew J. Francis and Contributors of the Bobbin Project
 * This file is distributed under the MIT licence. See the LICENCE file for further information.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] unencodedBytes = new byte[] { 34, 35, 36, 37, 37, 37, 67,
                68, 69 };
        System.out.println(hexencode(unencodedBytes));
    }

    /**
     * Encodes an array of plain bytes into a hex encoded string
     *
     * @param unencodedBytes The bytes to encode
     * @return A hex encoded string
     */
    public static String hexencode(byte[] unencodedBytes) {

        StringBuffer buffer = new StringBuffer();

        for (int i = 0; i < unencodedBytes.length; i++) {
            buffer.append(String.format("%02x", unencodedBytes[i]));
        }

        return buffer.toString();

    }
}

Related Tutorials