Can someone help me with the conversion of this python function to a vb.net function?
"""Function to convert a decimal integer to four byte doubleword"""
__version__ = '1.0.0'
def ddConverter(decimalInput):
'''
Takes a decimal integer and returns a list of decimal byte values\n
100 -> 0000.0000.0000.0000.0000.0000.0110.0100 -> Byte22=0, Byte21=0, Byte20=0, Byte19=100\n
List is inverted for simplicity reasons: Byte19 = Index[0], Byte20 = Index[1] etc...
'''
if decimalInput < 0:
isNegativFlag = True
else:
isNegativFlag = False
byteDecimal = bin(decimalInput)
# pad with zeros to fill 4 bytes
if isNegativFlag:
fourByteDecimal = str(byteDecimal[3:]).zfill(32)
else:
fourByteDecimal = str(byteDecimal[2:]).zfill(32)
# seperate in four bytes
if isNegativFlag:
byte22 = 255 - int(fourByteDecimal[:8], 2)
byte21 = 255 - int(fourByteDecimal[9:16], 2)
byte20 = 255 - int(fourByteDecimal[17:24], 2)
byte19 = 256 - int(fourByteDecimal[24:], 2)
else:
byte22 = int(fourByteDecimal[:8], 2)
byte21 = int(fourByteDecimal[9:16], 2)
byte20 = int(fourByteDecimal[17:24], 2)
byte19 = int(fourByteDecimal[24:], 2)
decimalList = (byte19, byte20, byte21, byte22)
return decimalList
I will use this code to generate a modus TCP telegram for the Igus D& drive. the last 4 bytes are uset to set the new position of the servo. This code converts a decimal into a double word decimal.
The user documentation of the igus D1 drive:
igus® Technical Documents[
^]
This code is used to generate the last 4 bytes 'byte 19,byte 20,byte 21,byte 22' of the modbus TCP telegram.
CONVERSION DECIMAL INTO DOUBLE WORD DECIMAL
INTEGER AS DOUBLE WORD
|INTEGER | BINARY |BYTE22 BYTE21 BYTE20 BYTE19|
|2OOO | OOOO.0000.0000.0000.0000.0111.1101.0000| 0 0 7 208 |
|100 | OOOO.0000.0000.0000.0000.0000.0000.0100| 0 0 0 100 |
|6000 | OOOO.0000.0000.0000.0001.0111.0111.0000| 0 0 23 112 |
What I have tried:
Public Shared Function ddConverter(ByVal decimalInput As Object) As Object
' Takes a decimal integer And returns a list of decimal byte values" + vbNewLine + "
'100 -> 0000.0000.0000.0000.0000.0000.0110.0100 -> Byte22=0, Byte21=0, Byte20=0, Byte19=100" + vbNewLine + "
Dim decimalList() As Integer
Dim byte19 As Object
Dim byte20 As Object
Dim byte21 As Object
Dim byte22 As Object
Dim fourByteDecimal As Object
Dim isNegativFlag As Object
If decimalInput < 0 Then
isNegativFlag = True
Else
isNegativFlag = False
End If
Dim byteDecimal = ParseBinary(decimalInput)
If isNegativFlag Then
fourByteDecimal = byteDecimal(3).ToString().zfill(32)
Else
fourByteDecimal = byteDecimal(2).ToString().zfill(32)
End If
If isNegativFlag Then
byte22 = 255 - Convert.ToInt32(fourByteDecimal(_._, 8), 2)
byte21 = 255 - Convert.ToInt32(fourByteDecimal(9, 16), 2)
byte20 = 255 - Convert.ToInt32(fourByteDecimal(17, 24), 2)
byte19 = 256 - Convert.ToInt32(fourByteDecimal(24), 2)
Else
byte22 = Convert.ToInt32(fourByteDecimal(_._, 8), 2)
byte21 = Convert.ToInt32(fourByteDecimal(9, 16), 2)
byte20 = Convert.ToInt32(fourByteDecimal(17, 24), 2)
byte19 = Convert.ToInt32(fourByteDecimal(24), 2)
End If
decimalList = {byte19, byte20, byte21, byte22}
Return decimalList
End Function