fork download
import java.util.Random;

/*
プログラミングのお題スレ Part15 
//mevius.5ch.net/test/read.cgi/tech/1564310397/257

257 名前:デフォルトの名無しさん[sage] 投稿日:2019/09/01(日) 18:40:58.06 ID:lGQcNl0x
お題: nバイトのデータxをビット列にして出力しなさい
 */
class Ideone
{
    public static void main(String[] args)
    {
        int n = 24;
        byte[] x = generateData(n);
        System.out.printf("0x%s: %s%n", toHexString(x), toBinaryString(x));
    }
    
    // nバイトのランダムなデータを作る
    static byte[] generateData(int n)
    {
        byte[] temp = new byte[n];
        new Random().nextBytes(temp);
        return temp;
    }
    
    static String toBinaryString(byte[] bytes)
    {
        StringBuilder sb = new StringBuilder(bytes.length * 8);
        for (byte b : bytes)
            for (int i = 7; i >= 0; i--)
                sb.append(b >>> i & 1);
        return sb.toString();
    }
    
    static String toHexString(byte[] bytes)
    {
        StringBuilder sb = new StringBuilder(bytes.length * 2);
        for (byte b : bytes)
            sb.append(Integer.toHexString(b & 0xFF));
        return sb.toString();
    }
}
Success #stdin #stdout 0.08s 33960KB
stdin
Standard input is empty
stdout
0x4cd72de1891bc91e98eca5512b96145516711cae31aeb8a: 010011001101011100101101111000011000100100011011110010010001111010011000111011001010010100000101000100101011100101100001010001010101000101100111000100011100101011100011000110101110101110001010