|
-
Sep 28th, 2009, 06:32 AM
#1
Thread Starter
Hyperactive Member
[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!
-
Sep 28th, 2009, 06:50 AM
#2
Re: Decimal to Binary
The Convert.ToString and Convert.ToInt16 methods allow you to convert between a Short value and a string representation of that number in base 2, 8, 10 or 16. Check out the MSDN documentation for more info.
-
Sep 28th, 2009, 06:55 AM
#3
Thread Starter
Hyperactive Member
Re: Decimal to Binary
 Originally Posted by jmcilhinney
The Convert.ToString and Convert.ToInt16 methods allow you to convert between a Short value and a string representation of that number in base 2, 8, 10 or 16. Check out the MSDN documentation for more info.
What do you mean with "base"? And I don't want to convert the short to a int16. Short is the same as int16. :S
-
Sep 28th, 2009, 06:57 AM
#4
Re: Decimal to Binary
 Originally Posted by gonzalioz
What do you mean with "base"? And I don't want to convert the short to a string or a int16. Short is the same as int16. :S
Hey,
Here you go:
http://doit.ort.org/course/inforep/130.htm
Gary
-
Sep 28th, 2009, 07:13 AM
#5
Thread Starter
Hyperactive Member
Re: Decimal to Binary
Thnx for your replies. But I just don't get it. So I have to turn the Short into a base 2 string?
I looked up conver.tostring method on msdn but it's just a endless list how to convert a variable to a string. Nothing about base 2 or something like that.
-
Sep 28th, 2009, 07:15 AM
#6
Re: Decimal to Binary
 Originally Posted by gonzalioz
Short is the same as int16. :S
Gee, so it is. So maybe you would use Convert.ToString to convert the Short to a String and then Convert.ToInt16 to convert a String to a Short. Did you read the documentation?
-
Sep 28th, 2009, 07:22 AM
#7
Thread Starter
Hyperactive Member
Re: Decimal to Binary
I got it! I did read the documentation but I didn't understand it. Not good enough in english to fully understand all the words and term they use. But I found something on another forum using base 2 like you said as a keyword.
What I do now is this:
Dim value as Short = 12732
Dim temp as string = convert.toString(value,2)
temp now becomes the binary number.
Thnx for your help
-
Sep 28th, 2009, 07:37 AM
#8
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:
-
Sep 28th, 2009, 08:12 AM
#9
Fanatic Member
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.
-
Sep 28th, 2009, 08:19 AM
#10
Re: [RESOLVED] Decimal to Binary
because Short is a 16 bit data type so I expect to see 16 bits, surprisingly...
-
Sep 28th, 2009, 08:21 AM
#11
Re: [RESOLVED] Decimal to Binary
 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.
-
Sep 28th, 2009, 08:34 AM
#12
Thread Starter
Hyperactive Member
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?
-
Sep 28th, 2009, 08:35 AM
#13
Re: [RESOLVED] Decimal to Binary
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)
Last edited by chris128; Sep 28th, 2009 at 08:39 AM.
-
Sep 28th, 2009, 08:52 AM
#14
Thread Starter
Hyperactive Member
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.
-
Sep 28th, 2009, 09:01 AM
#15
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.
Last edited by chris128; Sep 28th, 2009 at 09:34 AM.
-
Sep 28th, 2009, 09:35 AM
#16
Thread Starter
Hyperactive Member
Re: [RESOLVED] Decimal to Binary
 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.
-
Sep 28th, 2009, 09:21 PM
#17
Re: [RESOLVED] Decimal to Binary
 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".
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|