I celle C3 prøv og skrive Binær tallet for 512 (00100000000) Når man skriver det ind så fjerner den de 2 nuller foran. Så den under C3 tal skriver 256.
Ser ud til at den er rigtig - MEN det forudsætter at du ALTID husker at skrive 12 tegn (en del af DecValue-funktionen?). I princippet kan du skrive 001 og så skriver den 512, og det er jo ikke helt korrekt :0)
Jeg skal lige høre om hvis jeg vil have den til at regne med tal større end 2047. Er det så i linjen Bin = Array(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024) herunder jeg skal rette?
Public Function BinValue(Værdi As Range) As String Dim TempBin As String Application.Volatile Bin = Array(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024) Tempvalue = Abs(Værdi) TempBin = "" For i = 10 To 0 Step -1 If Tempvalue / Bin(i) >= 1 Then TempBin = TempBin & "1" Tempvalue = (Tempvalue - Bin(i)) Else TempBin = TempBin & "0" End If Next If Værdi < 0 Then TempBin = "1" & TempBin Else TempBin = "0" & TempBin End If BinValue = TempBin End Function
Tak. Har jeg forstået det rigtig at hvis jeg selv vil rette den med større tal, så skal jeg i denne linje Bin = Array(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192) forsætte med 16384, 32768, osv.. Og det sammen i Bin = Array(8192, 4096, 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2, 1)
Public Function LongDec2Bin(ByVal nIn As Long, _ Optional nBits As Long = 0&) As Variant 'J.E. McGimpsey Dim nReqBits As Long Dim sOut As String Dim sBit As String Dim bNeg As Boolean Dim i As Long If nIn < 0& Then bNeg = True nIn = -(nIn + 1&) End If If nIn = 0& Then nReqBits = 1& Else nReqBits = Int(Log(nIn) / Log(2&)) + 1& - bNeg End If If nBits <= 0& Then nBits = nReqBits If nBits >= nReqBits Then If bNeg Then sOut = String(nBits, "1") sBit = "0" Else sOut = String(nBits, "0") sBit = "1" End If For i = nBits To (nBits - nReqBits + 1&) Step -1 If (nIn - 2& * (nIn \ 2&)) > 0 _ Then Mid(sOut, i, 1&) = sBit nIn = nIn \ 2& Next i LongDec2Bin = sOut Else LongDec2Bin = CVErr(xlErrNum) End If End Function
Public Function LongBin2Dec(BinVal As String) As String Dim iVal#, temp#, i%, Length%
Length = Len(BinVal) For i = 0 To Length - 1 temp = CInt(Mid(BinVal, Length - i, 1)) iVal = iVal + (temp * (2 ^ i)) Next i LongBin2Dec = iVal End Function
Har lavet det. Og når jeg har skrevet de 2 funktioner ind.
Public Function LongDec2Bin(ByVal nIn As Long, _ Optional nBits As Long = 0&) As Variant 'J.E. McGimpsey Dim nReqBits As Long Dim sOut As String Dim sBit As String Dim bNeg As Boolean Dim i As Long If nIn < 0& Then bNeg = True nIn = -(nIn + 1&) End If If nIn = 0& Then nReqBits = 1& Else nReqBits = Int(Log(nIn) / Log(2&)) + 1& - bNeg End If If nBits <= 0& Then nBits = nReqBits If nBits >= nReqBits Then If bNeg Then sOut = String(nBits, "1") sBit = "0" Else sOut = String(nBits, "0") sBit = "1" End If For i = nBits To (nBits - nReqBits + 1&) Step -1 If (nIn - 2& * (nIn \ 2&)) > 0 _ Then Mid(sOut, i, 1&) = sBit nIn = nIn \ 2& Next i LongDec2Bin = sOut Else LongDec2Bin = CVErr(xlErrNum) End If End Function
Public Function LongBin2Dec(BinVal As String) As String Dim iVal#, temp#, i%, Length%
Length = Len(BinVal) For i = 0 To Length - 1 temp = CInt(Mid(BinVal, Length - i, 1)) iVal = iVal + (temp * (2 ^ i)) Next i LongBin2Dec = iVal End Function
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.