|
-
Mar 14th, 2001, 07:24 AM
#1
Thread Starter
New Member
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
-
Mar 14th, 2001, 07:36 AM
#2
Fanatic Member
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.
-
Mar 14th, 2001, 11:03 AM
#3
Lively Member
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
-
Mar 14th, 2001, 11:31 AM
#4
Hyperactive Member
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
-
Apr 3rd, 2001, 03:53 AM
#5
that won't work right.
change num = num \ 2
to read like this:
num = int(num \ 2)
-
Apr 3rd, 2001, 05:15 AM
#6
Addicted Member
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.
-
Apr 3rd, 2001, 06:03 AM
#7
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.
-
Apr 3rd, 2001, 07:21 PM
#8
Addicted Member
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
-
Apr 3rd, 2001, 09:04 PM
#9
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.
-
Apr 4th, 2001, 05:51 AM
#10
New Member
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)
-
Apr 4th, 2001, 05:55 AM
#11
transcendental analytic
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.
-
Apr 4th, 2001, 06:10 AM
#12
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?
-
Apr 4th, 2001, 06:20 AM
#13
never mind. I figured it out with some debug.printing. If the number is greater than 9, it returns -1, else it returns 0.
-
Apr 4th, 2001, 07:53 AM
#14
transcendental analytic
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|