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
Printable View
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
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.
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
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
that won't work right.
change num = num \ 2
to read like this:
num = int(num \ 2)
No...he is correct...he is using \ and not /Quote:
change num = num \ 2
to read like this:
num = int(num \ 2)
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
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.Quote:
using 5/2 = 2.5
using 5\2 = 2
I wonder if that is documented anywhere.
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
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.
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)
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
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? :confused:
never mind. I figured it out with some debug.printing. If the number is greater than 9, it returns -1, else it returns 0.
yep, > and all other comparation operators return a boolean, which can be interpreted as a numeric expression with True=-1 and False=0