Results 1 to 21 of 21

Thread: [RESOLVED] Denary (Base 10) to Binary (Base 2)

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    Rotherham,UK
    Posts
    26

    Resolved [RESOLVED] Denary (Base 10) to Binary (Base 2)

    i am trying to convert Denary numbers to a binary value, using two's compliments, but the i am using is coming back wrong. any help will be greatly apreciated.

    Code:
    Option Explicit
    Public Function InputNumber(Numpass As Double) As String
    Dim Bintemp As String
        While Numpass >= 0 'while number is greater or equal to 1
            Bintemp = Bintemp & Numpass Mod 2 'Divides by 2 each time and collects the remainder
                If OddEven(Numpass) = True Then
                    Bintemp = Bintemp & "0"
                Else
                    Bintemp = Bintemp & "1"
                End If
          Numpass = (Numpass / 2)
          Wend
    InputNumber = Bintemp
    End Function
    Public Function OddEven(num As Integer) As Boolean 'true of false
        If num Mod 2 = 0 Then
            OddEven = True 'Even
        Else
            OddEven = False 'False
        End If
    End Function
    Public Sub Calculate()
    Dim StrBinary As String, temp As Integer
        If Form1.OptBinary.Value = True Then
            temp = Form1.txtNumber.Text
            StrBinary = StrReverse(InputNumber(temp))
            Form1.txtAnswer.Text = StrBinary
        End If
        If Form1.OptOctal.Value = True Then
            temp = Form1.txtNumber.Text
            StrBinary = Oct(temp)
            Form1.txtAnswer.Text = StrBinary
        End If
        If Form1.OptDenary.Value = True Then
            temp = Form1.txtNumber.Text
            StrBinary = temp
            Form1.txtAnswer.Text = StrBinary
        End If
        If Form1.OptHexidecimal.Value = True Then
            temp = Form1.txtNumber.Text
            StrBinary = Hex(temp)
            Form1.txtAnswer.Text = StrBinary
        End If
        
    End Sub
    Last edited by frozenpost; Jun 1st, 2007 at 10:59 AM.
    thanks Frozenpost

  2. #2
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Denary (Base 10) to Binary (Base 2)

    I find this odd.

    While Numpass >= 0 'while number is greater or equal to 1

    should be >=1 in the first place.

  3. #3
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Denary (Base 10) to Binary (Base 2)

    If you're talking about (for instance) turning 256 into 11111111 (converting base-10 values to binary true/false representations, base-2) then there's an easier way to do it

    Remember the following piece of information...It's been useful to me for many years :-)
    if denary and 256 then bit(8) =1
    if denary and 128 then bit(7) = 1
    if denary and 64 then bit(6) = 1
    (etc)
    if denary and 1 then bit(0) = 1


    Using the AND operator here is a very useful way to work out which bits should be noted...and you can make this shorter still if you write it all into a clever loop...if you *really* need me to, I could probably write a den2bin function for you :-)
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  4. #4
    New Member
    Join Date
    Jun 2007
    Posts
    3

    Re: Denary (Base 10) to Binary (Base 2)

    Im unsure what u mean smUX,
    but how could u display on the text box the binary value or something like

    24 or 200?

    for example for 24 the answer should display on the textbox 11000

  5. #5
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Denary (Base 10) to Binary (Base 2)

    Well, bin(0) to bin(7) would have the binary values for the bytes, so if you added these together into a string backwards (7 to 0) then yes it would come out like "11000" if the number was 24 (and if 11000 is the binary representation, of course :-))

    Gimme 5 mins and I'll write a quick function that can handle the string building from denary to binary...back to denary is a simple matter, I'm sure :-)
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  6. #6
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Denary (Base 10) to Binary (Base 2)

    Okay, here it is :-)

    The function itself is:
    Code:
    Private Function den2bin(den As Double)
    sta = 16777216
    For b = 24 To 1 Step -1
    sta = sta / 2
    If den And sta Then x = x & "1" Else x = x & "0"
    Next b
    den2bin = x
    End Function
    and if you want to test it out natively (in a pre-designed form) you can download the attachment

    This function uses a slightly different method to the one I previously mentioned...this is the condensed version and can in theory handle any length of bits if you set sta (starting value)...24 bit has a max value of 16777216, which is what I have sta set as...if you want 32 bit, change sta to 4294967296, if you want 16 bit change it to 65536...etcetera :-)
    Attached Files Attached Files
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  7. #7
    New Member
    Join Date
    Jun 2007
    Posts
    3

    Re: Denary (Base 10) to Binary (Base 2)

    omg! its crazy!

    i dont understand any of this!

    can u do this in a simplier version?

    im only a college student!

  8. #8
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Denary (Base 10) to Binary (Base 2)

    Okay, I assume you're trying to learn from this...let me go a bit simpler :-)

    The AND operator in this case looks at the number on a binary level...if you do "24 and 1 it returns false because the number at the "1" point in the binary equivalent "11000" is 0 (the 5th digit). If you however did "24 and 16" or "24 and 8" you would get true, because those bits in the byte are 1s...it's just the way it works in most programming languages, and I've brought that nugget of info back from my Commodore 64/Commodore +4 programming days (yes, rhinobull, I've been programming that long...I'm sure you still think I've only been at it a few years...I started programming a little under 20 years ago and I'm not even 30 yet...just always have been amateur and haven't wanted to go pro or learn what I needed to :-P)

    Master or Frozen, if you have any more questions feel free to ask
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    Rotherham,UK
    Posts
    26

    Re: Denary (Base 10) to Binary (Base 2)

    ok i understand a bit then, sta is the starting possition and the den is the denary value. what is x and b
    thanks Frozenpost

  10. #10
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Denary (Base 10) to Binary (Base 2)

    Smux, that's the question you'd get for not properly declaring variables. As a general rule, put Option Explicit.

  11. #11
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Denary (Base 10) to Binary (Base 2)

    And I quote:
    Quote Originally Posted by smUX
    Gimme 5 mins and I'll write a quick function that can handle the string building from denary to binary...back to denary is a simple matter, I'm sure :-)
    Quick means I don't add in tons of declarations and comments...just the code that makes it work :-)

    I was going to also write in a bit of code to trim the leading 0s, but decided against it and kept it simple :-P

    Frozen, X and B are just temporary variables that I am using in the code...you should be able to scan through the *few* lines of code to work out where they're being used :-P
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  12. #12
    New Member
    Join Date
    Jun 2007
    Posts
    3

    Re: Denary (Base 10) to Binary (Base 2)

    is there any other way of doing this?

    cus the design work (flow charts) i did

    it goes

    Start

    Input Number

    Select Which Base

    Calculate

    Display Answer

    End

    im thinking thats right, the way ur saying seems right but

    i've never used that before...

    i dont really understand... i only know very very basic of VB :S

  13. #13
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Denary (Base 10) to Binary (Base 2)

    However, here's a more detailed version with the right stuff in there

    Code:
    Private Function den2bin(den As Double)
    dim sta as double, x as string
    'Sta = starting value.  The value in this case is a 24-bit number and the value
    'of the 25th bit...it is divided by 2 at each point and each successive number
    'is the value of the next lowest bit...sta/2 is the 24th bit, /2 again is the 23rd
    'until you get to 1 which is the 1st bit
    sta = 16777216
    For b = 1 To 24
    sta = sta / 2
    'X here is unimportant...it's a temporary string storing the binary data as it
    'is being built
    If den And sta Then x = x & "1" Else x = x & "0"
    Next b
    'This is how functions work.  Notice how the first part "den2bin" is the name
    'of this function...saying "den2bin = x" is saying to return the value of x as
    'the returned value from the function...functions return values :-P
    den2bin = x
    End Function
    Yes, I removed the step -1 and made it 1 to 24...I originally was going to use the original idea with the array but decided against it :-)
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  14. #14
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Denary (Base 10) to Binary (Base 2)

    Master, this code would only be useful for base 2 (binary) but I am sure there's ways of doing different bases...possibly using a modified version of this bit of code...as I've not worked with anything but base 2 and base 10, I've no idea how feasible it would be and if so how *easy* it would be :-)
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    Rotherham,UK
    Posts
    26

    Re: Denary (Base 10) to Binary (Base 2)

    could you just explan
    Code:
    For b = 16 To 1 Step -1
    sta = sta / 2
    If den And sta Then x = x & "1" Else x = x & "0"
    Next b
    then i abit more detail
    especally
    Code:
    If den And sta Then x = x & "1" Else x = x & "0"
    thanks Frozenpost

  16. #16
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Denary (Base 10) to Binary (Base 2)

    The first one is 24 to 1 step -1 not 16, but that's not an issue...the for/next is a loop which repeats the code within 24 times

    Sta is divided by 2 at each iteration, dropping from 16.7m down to 1 at the 24th interval...and at each interval that's what the bit at that point is valued at. At each point, if the base 2 value would be 1 it adds the character "1" to the string X and if the base 2 value would be 0 it adds the character "0" to the string

    the "den and sta" thing is simple. "den" is the denary number (the proper base 10 number we're converting from) and "sta" is the binary bit we're looking at...if the bit is there, "den and sta" would return true and would do the first bit...if not it would do the "else" bit of the line
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Location
    Rotherham,UK
    Posts
    26

    Re: Denary (Base 10) to Binary (Base 2)

    thanx i understand now, it has really helped
    thanks Frozenpost

  18. #18
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: Denary (Base 10) to Binary (Base 2)

    Glad to hear it...I help where I can :-)

    Don't forget to mark this thread as resolved if you feel you've got the info you need, so others know they can concentrate on ones that haven't been resolved :-P
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  19. #19
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: [RESOLVED] Denary (Base 10) to Binary (Base 2)

    Okay, people weren't expecting this, but here's an updated version which makes use of *ANY* base, it can do base 2 (binary), base 3 (trinary) and all the others...it makes use of a simple enough trick which I am sure most people here can work out...it *isn't* fast, I've not optimized the code in any way yet, although I dunno if I need to because it isn't like it's too slow to use...when I say it isn't fast, I mean it's unoptimized :-P

    Anyway, first use http://www.mathsisfun.com/numbers/convert-base.php and get yourself a test value if you want to test this. Me, I used 100 and base 3 which should return 10201 in trinary and it seems to work fine

    Here's the updated function, and notice it now needs to be sent more than just the source number now through the command!

    Code:
    Private Function den2bin(den As Double, base As Single, bits As Single)
    sta = (Val(base) ^ Val(bits)): den = den + 1: If sta < den Then MsgBox "ERROR!  Number exceeds bit/base setting values!": Exit Function
    For b = Val(bits) To 1 Step -1
    sta = sta / base
    By = 0
    For c = 1 To Val(base)
    If den > sta Then den = den - sta: By = By + 1
    Next c
    x = x & Trim(Str(By))
    Next b
    den2bin = x
    End Function
    And the whole thing in a module you can test it with...attached.

    If you enjoy, you know where my "reputation" link is :-P

    Remember I have *NOT* optimized this in any way...it could be improved in a few ways
    Attached Files Attached Files
    Well, everyone else has been doing it :-)
    Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
    Expect more to come in future
    If I have helped you, RATE ME! :-)

    I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!

    And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.

  20. #20
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: [RESOLVED] Denary (Base 10) to Binary (Base 2)

    Here's something I whipped up. It doesn't support negative numbers, but it's rather nifty just the same.
    Code:
    Public Function AnyBase(ByVal Number As Long, ByVal Base As Long) As String
      Dim Ret As String
      Dim Digit As Long
      
      If Base < 2 Or Base > 36 Then
        MsgBox "Invalid base", vbOKOnly, "AnyBase"
        Exit Function
      End If
      If Number = 0 Then Ret = "0"
      Do While Number > 0
        Digit = Number Mod Base
        If Digit < 10 Then
          Ret = Chr$(48 + Digit) & Ret
        Else
          Ret = Chr$(55 + Digit) & Ret
        End If
        Number = Number \ Base
      Loop
      AnyBase = Ret
    End Function

  21. #21
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: [RESOLVED] Denary (Base 10) to Binary (Base 2)

    Another binary to decimal (string manipulation rather than 2^n calculation).

    http://www.vbforums.com/showthread.p...&highlight=hex

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