[RESOLVED] Decimal to Binary
Hi all!
I need to convert a Short to his binary equivalent. For example:
Dim x As Short = 20480
And then I need a method to get the binary thingy, which in this case is: 101000000000000
I searched for a answer using google, but I couldn't find a proper explanaition.
Thnx!
Re: [RESOLVED] Decimal to Binary
Although that does work, it seems to only show the bits after the most significant bit in your number, which may not be a problem for you but for some people wanting to do the same thing it might be. For example, if I wanted to see the binary representation of the decimal number 256 I would expect it to look like this:
but using that code you would see this:
Re: [RESOLVED] Decimal to Binary
Why would you expect to see the binary representation like 00000001 00000000? If you do it manually you'll see that the representation is 100000000.
Re: [RESOLVED] Decimal to Binary
because Short is a 16 bit data type so I expect to see 16 bits, surprisingly...
Re: [RESOLVED] Decimal to Binary
Quote:
Originally Posted by
chris128
Although that does work, it seems to only show the bits after the most significant bit in your number, which may not be a problem for you but for some people wanting to do the same thing it might be. For example, if I wanted to see the binary representation of the decimal number 256 I would expect it to look like this:
but using that code you would see this:
Either is legitimate I guess, depending what you specifically want. We don't pad decimal numbers with 0's so we don't necessarily have to pad binary numbers either. Also, you say the decimal number 256, not the decimal number 256S, so it should be four bytes, not two. If you really do want the binary string padded then you've always got String.PadLeft at your disposal.
Re: [RESOLVED] Decimal to Binary
I use a while loop to add the missing 0's. Like this:
Dim temp = Convert.ToString(data, 2)
While temp.Length < 15
temp = "0" & temp
End While
Is that a slow method?
Re: [RESOLVED] Decimal to Binary
Quote:
Either is legitimate I guess, depending what you specifically want. We don't pad decimal numbers with 0's so we don't necessarily have to pad binary numbers either. Also, you say the decimal number 256, not the decimal number 256S, so it should be four bytes, not two. If you really do want the binary string padded then you've always got String.PadLeft at your disposal.
Well as we are talking about converting a Short to binary I didnt think I really needed to specify that I meant 2 bytes instead of 4 :)
As for the padding, I've just always seen bytes represented that way (ie in full) so that is how I would want to see it. Like I said in my first post, it may not be a problem for the OP but I just mentioned it in case others were expecting it to give the full bytes like I was.
EDIT: Looks like the OP expects to see it this way as well :)
I think you can use PadLeft as JMC mentioned, rather than using a loop. Example:
Code:
temp.PadLeft(16, "0"c)
Re: [RESOLVED] Decimal to Binary
Ok but I have to use 15 instead of 16 right?
Because the max of a int16 (short) is:
32767
1111 1111 1111 111
that's 15 times a 1.
Re: [RESOLVED] Decimal to Binary
Well there are 16 bits in a Int16/Short. However, I assume the reason why the maximum value is 32767 (which as you quite rightly point out, only requires 15 bits to be set) is because one bit has to be used to 'sign' it as a positive or negative value. If you use a UShort (Unsigned short) then the max value is 65535, which does use the full 16 bits :)
EDIT:
A signed Short of -1 actually equals this in Binary (which is why you cant have 65535 as the max value of a signed Short, because it would have the same binary value as this):
Of course that same binary value in a UShort would equal 65535.
It works slightly different to how I imagined but still the signing as positive or negative is what means that the max value for a signed Short is a lot less than the unsigned Short.
Re: [RESOLVED] Decimal to Binary
Quote:
Originally Posted by
chris128
Well there are 16 bits in a Int16/Short. However, I assume the reason why the maximum value is 32767 (which as you quite rightly point out, only requires 15 bits to be set) is because one bit has to be used to 'sign' it as a positive or negative value. If you use a UShort (Unsigned short) then the max value is 65535, which does use the full 16 bits :)
Thanks a lot ! That's very interesting and useful information. Thnx again.
Re: [RESOLVED] Decimal to Binary
Quote:
Originally Posted by
chris128
It works slightly different to how I imagined but still the signing as positive or negative is what means that the max value for a signed Short is a lot less than the unsigned Short.
For an explanation of how that signing works, search for the term "two's complement".