newm4n

Equals to this in C# String s = String.join(String.Empty,

Feb 10th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.88 KB | None | 0 0
  1.  public static void main(String[] args) {
  2.         System.out.println(doIt(new byte[] {1} ));
  3.         System.out.println(doIt(new byte[] {2} ));
  4.         System.out.println(doIt(new byte[] {1,1} ));
  5.         System.out.println(doIt(new byte[] {1,2} ));
  6.     }
  7.  
  8.     public static String paddLeft(String source, char padding, int length) {
  9.         StringBuilder sb = new StringBuilder();
  10.         sb.append(source);
  11.         while(sb.length() < length) {
  12.             sb.insert(0, padding);
  13.         }
  14.         return sb.toString();
  15.     }
  16.  
  17.     public static String convertToString(int number) {
  18.         return Integer.toBinaryString(number);
  19.     }
  20.  
  21.  
  22.     public static String doIt(byte[] bytes) {
  23.         StringBuilder sb = new StringBuilder();
  24.         for(byte b : bytes) {
  25.             sb.append(paddLeft(convertToString(b), '0', 8));
  26.         }
  27.         return sb.toString();
  28.     }
Advertisement
Add Comment
Please, Sign In to add comment