Results 1 to 14 of 14

Thread: convert binary

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2001
    Location
    Sydney
    Posts
    1

    Question

    I am stuck on this one

    Write a programme that converts decimal numbers to binary.

    Once you'r done with that

    write a programme that converts binary to decimal

    Cheers

  2. #2
    Fanatic Member simonm's Avatar
    Join Date
    Sep 2000
    Location
    Devon, England
    Posts
    796

    Thumbs up

    A binary string such as:

    '1101'

    is converted to decimal by sequentially working your way from right to left.

    Each digit is worth (2^n) * [DigitValue]

    NB: n = ordinal position in string from right (zero-based).

    Add up the 'worth' of each digit and that's your decimal value.

    i.e. With the above example:

    (2^0) * 1 + (2^1) * 0 + (2^2) * 1 + (2^3) * 1 =

    1 + 0 + 4 + 8 = 13.

  3. #3
    Lively Member
    Join Date
    Feb 2001
    Posts
    102

    Cool

    Hello!

    To go from decimal to binary, its exactly reversed.

    You take your decimal number, for example, 13.

    Take that - Divide it by 2
    (the base of binary and keep the remainders)
    Keep dividing the number you get by 2 until you get '0'

    1) 13 % 2 = 6 - Remainder of 1
    2) 6 % 2 = 3 - Remainder of 0
    3) 3 % 2 = 1 - Remainder of 1
    4) 1 % 2 = 0 - Remainder of 1

    No read it backwards in order of steps,

    You get 1101 = 13

    Im too lazy to put into code but maybe later
    Im sure you can figure it out

  4. #4
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372

    Seeing as everyone is too lazy!

    Code:
    Private Sub Command1_Click()
    Dim Number As Integer
    Dim Binary_String As String
    
    Number = Val(Text1.Text)
    Binary_String = ""
    While Number > 0
        Binary_String = Str(Number Mod 2) & Binary_String
        Number = Number \ 2
    Wend
    MsgBox Binary_String
    End Sub

  5. #5
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    that won't work right.

    change num = num \ 2
    to read like this:
    num = int(num \ 2)
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  6. #6
    Addicted Member Active's Avatar
    Join Date
    Jan 2001
    Location
    Lat: 13° 4' 46" N, Long: 80° 15' 20" E
    Posts
    209
    change num = num \ 2
    to read like this:
    num = int(num \ 2)
    No...he is correct...he is using \ and not /
    which is regular division

    using 5/2 = 2.5
    using 5\2 = 2

    ie...it rounds it to an integer so no need for int function
    Last edited by Active; Apr 3rd, 2001 at 07:13 PM.
    If you can't beat your computer at chess, try kickboxing !!!
    [Download Tag Editing Tools.]

  7. #7
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    using 5/2 = 2.5
    using 5\2 = 2
    Whoah. I didn't know that. Maybe that could explain some funny number results i have gotten with my programs. I am all the time using the wrong sign on accident.

    I wonder if that is documented anywhere.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  8. #8
    Addicted Member Active's Avatar
    Join Date
    Jan 2001
    Location
    Lat: 13° 4' 46" N, Long: 80° 15' 20" E
    Posts
    209
    There is a Slight problem when using \

    Don't try 2.5687\4.58588 ..it may completely be wrong

    In short it is useful only if both numerator and denominator are both integers.


    I made a Javascript Binary Encoder/Decoder that translates
    Ascii Characters in to a String of 1's and 0's some months ago.

    http://forums.vb-world.net/attachmen...attachmentid=4
    If you can't beat your computer at chess, try kickboxing !!!
    [Download Tag Editing Tools.]

  9. #9
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    whoah. New interface.
    dd
    So thats how they did it...
    Anyway, i also wrote one in vb a couple of days ago that converted the number to octal and then read the bits from the new octal. I think this way will be a little faster though.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  10. #10
    New Member star's Avatar
    Join Date
    Mar 2001
    Location
    In the distant future...
    Posts
    9
    note, integer division is only fast when you divide two INTEGERS, since to convert floating point values to integers is usually slows down the process even more that it would be to divide two floating points.

    to do

    2.5687\4.58588

    is like to do 3\5 which is 0, (and the rest is of course 3 mod 5 which is 3)
    It's astronomical!

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Here's two string/numeric based radix convertion functions i wrote
    Code:
    Function Base2Dec(val$, base%): Dim n%, a() As Byte
        a = StrConv(UCase(val), vbFromUnicode)
        For n = 1 To Len(val)
            Base2Dec = CDec(Base2Dec + (a(n - 1) - 48 + 7 * (a(n - 1) > 64)) * base ^ (Len(val) - n))
        Next n
    End Function
    Function Dec2Base$(val, base%): Dim n%, s
        For n = 0 To 100
            s = Int(val / base ^ n)
            If s = 0 Then Exit For
            s = s - Int(s / base) * base 's mod base
            Dec2Base = Chr(s + 48 - 7 * (s > 9)) & Dec2Base
        Next n
    End Function
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  12. #12
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    i have a question about this line:
    Dec2Base = Chr(s + 48 - 7 * (s > 9)) & Dec2Base
    i thought > was a comparison operator. What does it do in this case?
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  13. #13
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628
    never mind. I figured it out with some debug.printing. If the number is greater than 9, it returns -1, else it returns 0.
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  14. #14
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    yep, > and all other comparation operators return a boolean, which can be interpreted as a numeric expression with True=-1 and False=0
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

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