binary to hex for int array - Java java.lang

Java examples for java.lang:int Array

Description

binary to hex for int array

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int[] binarray = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(binb2hex(binarray));
    }//from   w  w w. j  a  v a2s  .c  o m

    private static final boolean hexcase = false;

    private static String binb2hex(int[] binarray) {
        String hex_tab = hexcase ? "0123456789abcdef" : "0123456789abcdef";
        String str = "";

        for (int i = 0; i < binarray.length * 4; i++) {
            char a = (char) hex_tab
                    .charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xf);
            char b = (char) hex_tab
                    .charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xf);
            str += (new Character(a).toString() + new Character(b)
                    .toString());
        }
        return str;
    }
}

Related Tutorials