(Java) Specify number of bits (length) when converting binary number to string?

后端 未结 8 1200
有刺的猬
有刺的猬 2020-12-30 13:03

I\'m trying to store a number as a binary string in an array but I need to specify how many bits to store it as.

For example, if I need to store 0 with two bits I ne

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-30 13:45

    Use Integer.toBinaryString() then check the string length and prepend it with as many zeros as you need to make your desired length.

提交回复
热议问题