Click here to Skip to main content
15,566,522 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can someone help me with the conversion of this python function to a 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
        isNegativFlag = False
    byteDecimal = bin(decimalInput)
    # pad with zeros to fill 4 bytes
    if isNegativFlag:
        fourByteDecimal = str(byteDecimal[3:]).zfill(32)
        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)
        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.

|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
           isNegativFlag = False
       End If

       Dim byteDecimal = ParseBinary(decimalInput)

       If isNegativFlag Then
           fourByteDecimal = byteDecimal(3).ToString().zfill(32)
           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)
           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
Updated 9-Jan-21 3:59am
Gerry Schmitz 8-Jan-21 11:09am    
You need to say what the point is because a "decimal" in .NET is 16 bytes.

And if you think 100 -> 0000.0000. etc. means something ... it doesn't.
Richard MacCutchan 9-Jan-21 7:57am    
Why do you need a conversion? Does the documentation actually specify that you need to do this, or does it explain how to generate the values?
nicoheylen 9-Jan-21 8:39am    
It explains only how to generate the values.
I would like to convert the decimal value to a 4 bytes value

This is not a code conversion service: we are not here to translate code for you.
Even if we did, what you would end up with would not be "good code" in the target language – they are based on very different frameworks, and what makes something work in one language does not always "translate" directly into another.
So what you end up with is very poor code, that is difficult if not impossible to maintain, that can’t be upgraded nicely, and that will cause you immense headaches if the original is changed. And it’ll be a nightmare to debug if it doesn’t work "straight out of the box".
Instead, use the source code as a specification for a new app written in and for the target language / framework and write it from scratch using the original as a "template". You will get a much, much better result that will save you a lot of time in the long run.

Start by looking at what the four bytes actually contain for a number of specific values, and what the integer value of that is made from. At a guess, the eight input values you show are in BCD and converting from BCD to hex based integer is pretty simple:
BCD to binary and hex.[^] gives you code in C# - which because it's using the .NET framework, translates pretty well even into VB
There are even online converters to do it for you: Code Converter C# to VB and VB to C# – Telerik[^]
Share this answer
The algorithm is not that complicated if you actually think about it:
Dim numbers = New Integer() { 2000, 100, 6000 }
' Iterate through the list.
Dim nexti As Integer
For Each item As Integer In numbers
    Console.Write($"{item}: ")
    Dim ans = New Integer() { 0, 0, 0, 0 }
    For j As Integer = 3 To 0 Step -1
        nexti = item Mod 256
        item = item \ 256 ' what a silly language is VB
        ans(j) = nexti
    Next j
    Console.WriteLine($"{ans(0)}, {ans(1)}, {ans(2)}, {ans(3)}")
Share this answer
nicoheylen 9-Jan-21 13:43pm    
works like a charm, thank you.
Can you also help me with reversing this ?
0,0,8,208 --> 2000
The system sends a status back in this format so I need to decode is back.
Richard MacCutchan 10-Jan-21 4:31am    
The reverse sum is even simpler. Start with a zero value, then for each digit multiply the total by 256 before adding in the digit.
' assume numbers contains the four digits 0, 0, 8, 208
Dim answer As Integer
answer = 0
For Each item As Integer In numbers
    answer = answer * 256
    answer = answer + item
nicoheylen 10-Jan-21 5:20am    
Thank you very much for the help.
Richard MacCutchan 10-Jan-21 6:38am    
You are welcome. But a piece of advice for the future. If you need to solve a problem, don't rely on code that you find on the internet, unless it comes with a detailed explanation. For examples of good code see the articles section here on CodeProject.
nicoheylen 10-Jan-21 8:45am    
I Will keep that in mind.
I tried the reverse sum but when I convert 0,0,8,208 I get 2256 as result in stead as 2000?
can you have look a it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900