|
-
May 1st, 2000, 09:32 PM
#1
Thread Starter
New Member
Help
I need to change a deciml number, say 28 into its equivialnt binary pattern 011100?
Any easy way?
Cheers in Advance
Andy
-
May 1st, 2000, 09:49 PM
#2
Fanatic Member
as in "is there a built in function?" not to my knoweledge. There is a hex() function though.
Just write you're own.
you have to decide on what output you want... do you want a string? do you want to be able to work with int's and longs too? do you need arrays?
The functions I write are usually to convert an array of bytes (which anything is convertable into) to an array of bits (held in a byte array 8 time longer)
If you just need to view single bytes, use strings. if you have to work with them (like when I wrote a vb dll for DES encrytpion) you need something more flexable (bit arrays)
In your function you can either use division by ^2 in a loop to find the 1's and 0's or you can AND a number with 1,2,4,8,16,32,64,128 and get the output.
If there is a better way I too would LOVE to hear about it
Paul
Paul Dwyer 
Network Engineer
Aussie In Tokyo
Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)
-
May 1st, 2000, 10:02 PM
#3
Hyperactive Member
Does this help....?
BINARY TO DEC/HEX
Code:
Public Function Bin2Dec(Bin) As Long
Y = Len(Bin) - 1
For X = 1 To Len(Bin)
Bin2Dec = Bin2Dec + ((2 ^ Y)) * (Mid$(Bin, X, 1))
Y = Y - 1
Next X
End Function
Private Sub Command1_Click()
Text2.Text = Hex(Bin2Dec(Text1.Text))
End Sub

Dan
-
May 1st, 2000, 10:04 PM
#4
Fanatic Member
The following code will convert a decimal number into a 8 bit binary. If you want to convert bigger numbers simply change the loop counter to the number of bits you want - 1.
Code:
Dim iNum As Integer
Dim iPos As Integer
Dim s As String
iNum = 28
For iPos = 0 To 7
If (iNum And (2 ^ iPos)) Then
s = "1" & s
Else
s = "0" & s
End If
Next iPos
MsgBox s
Iain, thats with an i by the way!
-
May 1st, 2000, 11:02 PM
#5
Frenzied Member
wouldn't
Code:
Private Function CBin(x As Long) As String
Dim strHex As String
Dim strBin As String
Dim i As Long
x = Text1.Text
strHex = Hex(x)
For i = 1 To Len(strHex)
Select Case Mid$(strHex, i, 1)
Case "0"
strBin = strBin & "0000"
Case "1"
strBin = strBin & "0001"
Case "2"
strBin = strBin & "0010"
Case "3"
strBin = strBin & "0011"
Case "4"
strBin = strBin & "0100"
Case "5"
strBin = strBin & "0101"
Case "6"
strBin = strBin & "0110"
Case "7"
strBin = strBin & "0111"
Case "8"
strBin = strBin & "1000"
Case "9"
strBin = strBin & "1001"
Case "A"
strBin = strBin & "1010"
Case "B"
strBin = strBin & "1011"
Case "C"
strBin = strBin & "1100"
Case "D"
strBin = strBin & "1101"
Case "E"
strBin = strBin & "1110"
Case "F"
strBin = strBin & "1111"
End Select
Next i
CBin = strBin
End Function
be better, select case statements are very fast and you can go pretty much as high as you like, in fact I tested it as high as it's go and it always worked. Minus numberswill come out wrong because of the way they're stored in memory.
-
May 2nd, 2000, 08:51 PM
#6
Lively Member
Geez, you guys write such LONG code for such simple things (referring to the Select Case deal).
You may notice I like the 'Long' type. That's because it's the VB equivalent to a 32-bit int.
Code:
Public Function CvtBase(ByRef org$, Optional ByVal frombase As Long = 10, Optional ByVal tobase As Long = 10) As String
Dim base10n As Long, map$, i As Long, pos As Long, out$
map$ = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
For i = 1 To Len(org$)
pos = InStr(map$, UCase$(Mid$(org$, i, 1))) - 1
base10n = base10n * frombase + pos
Next
While (base10n <> 0)
pos = base10n Mod tobase: base10n = base10n \ tobase
out$ = Mid$(map$, 1 + pos, 1) & out$
Wend
CvtBase = out$
End Function
Print CvtBase("12345", 10, 2)
>> 11000000111001
- Steve
Real programmers use COPY CON PROGRAM.EXE
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
|