fork download
  1. import java.util.Random;
  2.  
  3. /*
  4. プログラミングのお題スレ Part15
  5. //mevius.5ch.net/test/read.cgi/tech/1564310397/257
  6.  
  7. 257 名前:デフォルトの名無しさん[sage] 投稿日:2019/09/01(日) 18:40:58.06 ID:lGQcNl0x
  8. お題: nバイトのデータxをビット列にして出力しなさい
  9.  */
  10. class Ideone
  11. {
  12. public static void main(String[] args)
  13. {
  14. int n = 24;
  15. byte[] x = generateData(n);
  16. System.out.printf("0x%s: %s%n", toHexString(x), toBinaryString(x));
  17. }
  18.  
  19. // nバイトのランダムなデータを作る
  20. static byte[] generateData(int n)
  21. {
  22. byte[] temp = new byte[n];
  23. new Random().nextBytes(temp);
  24. return temp;
  25. }
  26.  
  27. static String toBinaryString(byte[] bytes)
  28. {
  29. StringBuilder sb = new StringBuilder(bytes.length * 8);
  30. for (byte b : bytes)
  31. for (int i = 7; i >= 0; i--)
  32. sb.append(b >>> i & 1);
  33. return sb.toString();
  34. }
  35.  
  36. static String toHexString(byte[] bytes)
  37. {
  38. StringBuilder sb = new StringBuilder(bytes.length * 2);
  39. for (byte b : bytes)
  40. sb.append(Integer.toHexString(b & 0xFF));
  41. return sb.toString();
  42. }
  43. }
  44.  
Success #stdin #stdout 0.08s 33960KB
stdin
Standard input is empty
stdout
0x4cd72de1891bc91e98eca5512b96145516711cae31aeb8a: 010011001101011100101101111000011000100100011011110010010001111010011000111011001010010100000101000100101011100101100001010001010101000101100111000100011100101011100011000110101110101110001010