# How to convert 16 bit binary number to an integer troubleshooting

Something like this size is the number of bits:. 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. 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 1 s. In terms of an algorithm to convert base10 numbers to binary, I personally think the following is pretty straightforward:. Binary is a representation and not a format to convert an integer to. For example, if you have an integer:. The binary representation will be Java has only signed integers, so this link will be helpful. By posting your answer, you agree to the privacy policy and terms of service. Email Sign Up or sign in with Google.

How to convert a long to a fixed-length bit binary string? Is it a String of 1s and 0s that you want as the output? A short in Java is still signed, though. I saw 16 bits and was thinking short. Another option is to set an int value to and simply divide this by 2 each time you go through the loop. This will give you the same value as your POW but do away with floating point maths. Once you have the value then the post from bko will work fine.

To give you a complete overview: Using these two parts I intend to convert it to a decimal value e. I would then use the decimal value to calculate the correction factor e.

Please advise if I am approaching it in the right way my first post has the part of the code or any better ways to solve this. When I compile it I get an error: Could someone please suggest where I could be going wrong? I compiled the code, adding 0b in front of the binary number, and the compiler error I get is. To make this function available to you, please add.

Note the cast to char so that you only access a single byte and not the complete array. How do I convert this to a decimal number my approach can be seen in the first post , as I need to use the decimal number in a formula. Yes, the problem there is you are trying to publish an event with event data decimalValue which is an int, but the function expects a string.