How to get 0-padded binary representation of an integer in java?

前端 未结 17 1913
我在风中等你
我在风中等你 2020-11-22 08:23

for example, for 1, 2, 128, 256 the output can be (16 digits):

0000000000000001
0000000000000010
0000000010000000
0000000100000000
相关标签:
17条回答
  • 2020-11-22 08:49

    try...

    String.format("%016d\n", Integer.parseInt(Integer.toBinaryString(256)));
    

    I dont think this is the "correct" way to doing this... but it works :)

    0 讨论(0)
  • 2020-11-22 08:52

    I was trying all sorts of method calls that I haven't really used before to make this work, they worked with moderate success, until I thought of something that is so simple it just might work, and it did!

    I'm sure it's been thought of before, not sure if it's any good for long string of binary codes but it works fine for 16Bit strings. Hope it helps!! (Note second piece of code is improved)

    String binString = Integer.toBinaryString(256);
      while (binString.length() < 16) {    //pad with 16 0's
            binString = "0" + binString;
      }
    

    Thanks to Will on helping improve this answer to make it work with out a loop. This maybe a little clumsy but it works, please improve and comment back if you can....

    binString = Integer.toBinaryString(256);
    int length = 16 - binString.length();
    char[] padArray = new char[length];
    Arrays.fill(padArray, '0');
    String padString = new String(padArray);
    binString = padString + binString;
    
    0 讨论(0)
  • 2020-11-22 08:52

    Starting with Java 11, you can use the repeat(...) method:

    "0".repeat(Integer.numberOfLeadingZeros(i) - 16) + Integer.toBinaryString(i)
    

    Or, if you need 32-bit representation of any integer:

    "0".repeat(Integer.numberOfLeadingZeros(i != 0 ? i : 1)) + Integer.toBinaryString(i)
    
    0 讨论(0)
  • 2020-11-22 08:55

    I do not know "right" solution but I can suggest you a fast patch.

    String.format("%16s", Integer.toBinaryString(1)).replace(" ", "0");
    

    I have just tried it and saw that it works fine.

    0 讨论(0)
  • 2020-11-22 08:55

    This is an old trick, create a string with 16 0's then append the trimmed binary string you got from String.format("%s", Integer.toBinaryString(1)) and use the right-most 16 characters, lopping off any leading 0's. Better yet, make a function that lets you specify how long of a binary string you want. Of course there are probably a bazillion other ways to accomplish this including libraries, but I'm adding this post to help out a friend :)

    public class BinaryPrinter {
    
        public static void main(String[] args) {
            System.out.format("%d in binary is %s\n", 1, binaryString(1, 4));
            System.out.format("%d in binary is %s\n", 128, binaryString(128, 8));
            System.out.format("%d in binary is %s\n", 256, binaryString(256, 16));
        }
    
        public static String binaryString( final int number, final int binaryDigits ) {
            final String pattern = String.format( "%%0%dd", binaryDigits );
            final String padding = String.format( pattern, 0 );
            final String response = String.format( "%s%s", padding, Integer.toBinaryString(number) );
    
            System.out.format( "\npattern = '%s'\npadding = '%s'\nresponse = '%s'\n\n", pattern, padding, response );
    
            return response.substring( response.length() - binaryDigits );
        }
    }
    
    0 讨论(0)
提交回复
热议问题