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

前端 未结 8 1040
谎友^
谎友^ 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: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;
    

提交回复
热议问题