DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

vbs binary to decimal converter

I tried to use the following converter but it doesn`t work. Diadem says ')' expected in line 1. What is wrong with this code?

 

Option Explicit 'Erzwingt die explizite Deklaration aller Variablen in einem Script.
Public Function BinaryToDecimal(Binary As String) As Long
Dim n As Long
Dim s As Integer

For s = 1 To Len(Binary)
n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ _
(s - 1)))
Next s

BinaryToDecimal = n
End Function

Public Function DecimalToBinary(DecimalNum As Long) As _
String
Dim tmp As String
Dim n As Long

n = DecimalNum

tmp = Trim(Str(n Mod 2))
n = n \ 2

Do While n <> 0
tmp = Trim(Str(n Mod 2)) & tmp
n = n \ 2
Loop

DecimalToBinary = tmp
End Function

 

Thanks in advance!

0 Kudos
Message 1 of 4
(10,276 Views)

VBS differs in syntax from VB. Your method would look like this.

 

Option Explicit

Function BinaryToDecimal(Binary)
  Dim n
  Dim s
  
  For s = 1 To Len(Binary)
    n = n + (Mid(Binary, Len(Binary) - s + 1, 1) * (2 ^ (s - 1)))
  Next
  
  BinaryToDecimal = n

End Function

Function DecimalToBinary(DecimalNum)
  Dim tmp
  Dim n
  
  n = DecimalNum
  
  tmp = Trim(Str(n Mod 2))
  n = n \ 2
  
  Do While n <> 0
    tmp = Trim(Str(n Mod 2)) & tmp
    n = n \ 2
  Loop
  
  DecimalToBinary = tmp
End Function

MsgBox BinaryToDecimal("110")

 

0 Kudos
Message 2 of 4
(10,260 Views)

Hi musterman,

 

Here's the VBScript library of functions I created before DataPlugins shipped in DIAdem 9.1.

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 3 of 4
(10,244 Views)

Thanks for your solutions! I came a big step further through that. 

 

Now I´m nearly at the solution. I wrote following programm. 

The first part reads in special parts of a file from my pc into an array.

The second part converts the binary parts into hex-values. (here only meinArray(0), but I am going to insert a loop)

The third part converts the hex-values in 2`s complement into signed decimal values

 

The problem is only how to give the values from the second part into the third part. It doesn´t work with the following solution:

 

'first part
Dim fso, zeile, Textdatei, Zeichen1, Zeichen2, meinArray(128), i, txt
' Zugriff auf das Dateisysten
Set fso = CreateObject("Scripting.FileSystemObject")
Set Textdatei = fso.OpenTextFile("C:\Users\Christoph\Desktop\Projekt\11061543.003",1)
Textdatei.Skip(40960)

do
  
  Zeichen1 = Textdatei.Read(1)
  Zeichen2 = Textdatei.Read(1)
  meinArray(i)= Zeichen2 & Zeichen1
  i=i+1
loop while i<128

For i = LBound(meinArray) To UBound(meinArray)
    txt = txt & meinArray(i) & vbCrLf
Next 
  
  MsgBox txt


'second part
Function StrToHex(Values) ' useful for debugging!
  Dim j, k, TempVal, HexVals
  IF NOT IsArray(Values) THEN
    TempVal = Values
    ReDim Values(0)
    Values(0) = TempVal
  END IF ' the Values input is a Scaler-- map to a 1 element Array
  ReDim HexVals(UBound(Values))
  FOR j = 0 TO UBound(Values)
    HexVals(j) = ""
    FOR k = 0 TO Len(Values(j))-1
      HexVals(j) = HexVals(j) & Right("0" & Hex(Asc(Mid(Values(j), k+1, 1))), 2) 
    NEXT ' k
  NEXT ' j
  IF NOT IsEmpty(TempVal) THEN
    TempVal = HexVals(0)
    HexVals = TempVal
  END IF ' the Values input was a Scaler-- return a Scaler
StrToHex = HexVals ' returns a Variant array of [Hex][Codes][in][each][Value()]
End Function ' StrToHex()

StrToHex(meinArray(0))


'third part
Function TwoComplement8Bits(StrToHex)
    TwoComplement8Bits=CInt(StrToHex)
    If TwoComplement8Bits>127 Then 
        TwoComplement8Bits=-1*((Not (TwoComplement8Bits Or &hff00))+1)
    End If
    
End Function



MsgBox TwoComplement8Bits(StrToHex)

 

0 Kudos
Message 4 of 4
(10,234 Views)