bytes To Decoded Hex String - Android java.lang

Android examples for java.lang:String Unicode

Description

bytes To Decoded Hex String

Demo Code


//package com.book2s;

public class Main {
    private static char[] hexArray = "0123456789ABCDEF".toCharArray();

    public static String bytesToDecodedHexString(byte[] bytes) {
        return decodeHexString(bytesToHexString(bytes));
    }//  w w w  . ja v a2s.co  m

    public static String decodeHexString(String hexString) {
        StringBuilder sb = new StringBuilder();
        char[] hexData = hexString.toCharArray();
        for (int count = 0; count < hexData.length - 1; count += 2) {
            int firstDigit = Character.digit(hexData[count], 16);
            int secondDigit = Character.digit(hexData[count + 1], 16);
            int decimal = firstDigit * 16 + secondDigit;
            sb.append((char) decimal);
        }
        return sb.toString();
    }

    public static String bytesToHexString(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        for (int j = 0; j < bytes.length; j++) {
            int v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }
}

Related Tutorials