How to convert a long to a fixed-length 16-bit binary string?

前端 未结 8 1015
谎友^
谎友^ 2020-12-10 17:23

Hi i want to convert a long integer to binary but the problem is i want a fixed 16 bit binary result after conversion like if i convert 2 to 16 bit binary it should give me

相关标签:
8条回答
  • 2020-12-10 17:41

    Integer.toBinaryString will convert an int to its binary representation as a string.

    It does not give you leading zeroes, so if you really need the string to have those and be 16 bits, you can just add them yourself.

    You should know how to do that.


    Do note that an int is actually 32 bits in Java. You should also know how two's complement works. The binary representation of -1, for example, is 32 1s.

    0 讨论(0)
  • if you want the binary representation of a long, then there is a method in the Long objet to do so :

    String Long.toString(long i, int radix);
    

    with a radix of 2, you should have a binary representation.

    regards
    Guillaume

    0 讨论(0)
  • 2020-12-10 17:43

    In contrast to many suggestions here: Integer.toBinaryString, doesn't work for a 16 bit (a short) and it will not print leading zero's. The reason is that (as the name suggests) this will only work for integers. And for negative numbers the bit representation will change (the first bit indicates a negative number). The two numbers below represent the same number in short and int. So if you want to represent the raw bits you have received (this is the general application of your problem), this function will generate strange output.

    decimal: -3
    short:                     1111 1111 1111 1101
    int:   1111 1111 1111 1111 1111 1111 1111 1101
    

    EDIT: Changed the number above

    Hence you can not cast the short if you are interested in the bit.

    Java doesn't provide the implementation for short, so you will have to provide your own. Something like this (size is the number of bits):

    int displayMask = 1 << (size - 1);
    StringBuffer buf = new StringBuffer( size);
    for ( int c = 1; c <= size; c++ ) 
    {
        buf.append( ( value & displayMask ) == 0 ? '0' : '1' );
        value <<= 1;
    }
    
    0 讨论(0)
  • In terms of an algorithm to convert base10 numbers to binary, I personally think the following is pretty straightforward:

    char[] array;
    
    for (i; i < 16; i++)
    {
        if (yourNumber % 2 == 0)
              array[16-i] = '0';
        else if (yourNumber % 2 == 1)
              array[16-i] = '1';
        yourNumber = yourNumber / 2;
    }
    

    You can then convert your char array to a String if you like.

    0 讨论(0)
  • 2020-12-10 17:48

    Binary is a representation and not a format to convert an integer to. For example, if you have an integer:

    int i = 2;
    

    The binary representation will be 00000010. Java has only signed integers, so this link will be helpful.

    0 讨论(0)
  • 2020-12-10 17:53

    I had to do it for a 32 bit number and ended up with:

    String stringWord = Long.toBinaryString(word);
    while (stringWord.length() < 32) // ensure that length of word is 32
            stringWord = "0" + stringWord;
    
    0 讨论(0)
提交回复
热议问题