binary to string - Java java.lang

Java examples for java.lang:Hex

Description

binary to string

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int[] bin = new int[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(binb2str(bin));
    }//www .  jav  a2s  .  co m

    private static final int chrsz = 8;

    private static String binb2str(int[] bin) {
        String str = "";
        int mask = (1 << chrsz) - 1;

        for (int i = 0; i < bin.length * 32; i += chrsz) {
            str += (char) ((bin[i >> 5] >>> (24 - i % 32)) & mask);
        }
        return str;
    }
}

Related Tutorials