Convert byte Chars To Hex String - Java java.lang

Java examples for java.lang:String Hex

Description

Convert byte Chars To Hex String

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        char[] arr = new char[] { 'b', 'o', 'o', 'k', '2', 's', '.', 'c',
                'o', 'm', 'a', '1', };
        System.out.println(byteCharsToHexString(arr));
    }/*  w w w .  j  a va2s  . c om*/

    public static String byteCharsToHexString(char[] arr) {
        StringBuilder strb = new StringBuilder();

        for (char ch : arr) {
            String hex = Integer.toHexString(ch);
            if (hex.length() == 1) {
                hex = "0" + hex;
            }
            strb.append(hex);
        }
        return strb.toString().toUpperCase();
    }

    public static String byteCharsToHexString(char[] arr, int offset,
            int length) {
        char[] tmp = new char[length];
        for (int i = offset; i < offset + length; i++) {
            tmp[i - offset] = arr[i];
        }
        return byteCharsToHexString(tmp);

    }
}

Related Tutorials