Results 1 to 17 of 17

Thread: [RESOLVED] Decimal to Binary

  1. #1

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Resolved [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!

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Re: Decimal to Binary

    Quote Originally Posted by jmcilhinney View Post
    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

  4. #4
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Decimal to Binary

    Quote Originally Posted by gonzalioz View Post
    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

  5. #5

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    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.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Decimal to Binary

    Quote Originally Posted by gonzalioz View Post
    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    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

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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:
    Code:
    00000001 00000000
    but using that code you would see this:
    Code:
    1 00000000
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    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.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

  10. #10
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [RESOLVED] Decimal to Binary

    because Short is a 16 bit data type so I expect to see 16 bits, surprisingly...
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Decimal to Binary

    Quote Originally Posted by chris128 View Post
    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:
    Code:
    00000001 00000000
    but using that code you would see this:
    Code:
    1 00000000
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    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?

  13. #13
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  14. #14

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    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.

  15. #15
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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):
    Code:
     11111111 11111111
    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.
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  16. #16

    Thread Starter
    Hyperactive Member gonzalioz's Avatar
    Join Date
    Sep 2009
    Location
    <body></body>
    Posts
    508

    Re: [RESOLVED] Decimal to Binary

    Quote Originally Posted by chris128 View Post
    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.

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] Decimal to Binary

    Quote Originally Posted by chris128 View Post
    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".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width