Skip to content

Conversation

Jodaille
Copy link
Contributor

I don't know how/why but it seems a conversion bug, an extra b is added :

'0bb1000000000000000000000000000000'

it should be
'0b1000000000000000000000000000000'

File "/Users/jodaille/ddt4allwithplugin/ecu.py", line 300, in build_data_stream
data.setValue(v, data_stream, datatitem, self.ecu_file.endianness)
File "/Users/jodaille/ddt4allwithplugin/ecu.py", line 643, in setValue
valueasint = int("0b" + requestasbin, 2)
^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: invalid literal for int() with base 2: '0bb1000000000000000000000000000000'

You can test in manual request :

Capture d’écran 2025-03-12 à 16 03 11

@Furtif
Copy link
Collaborator

Furtif commented Mar 12, 2025

The change in the pull request modifies the way the binary string is processed before converting it to an integer. To determine if this change is good, let's analyze the differences:

Original Code

requestasbin = "".join(requestasbin)
valueasint = int("0b" + requestasbin, 2)

Modified Code

requestasbin = "0b" + requestasbin.lstrip("b")
valueasint = int(requestasbin, 2)

Analysis

  1. Original Code:

    • Joins the list requestasbin into a single string.
    • Converts the joined binary string to an integer by prepending "0b" to it and using the int function with base 2.
  2. Modified Code:

    • Joins the list requestasbin into a single string.
    • Prepends "0b" to the joined string and removes any leading "b" characters from the string using lstrip("b").
    • Converts the modified binary string to an integer using the int function with base 2.

Potential Issues

  1. Handling Leading "b" Characters:

    • The original code assumes that the joined string does not start with a "b", as it directly prepends "0b" to the binary string.
    • The modified code accounts for the possibility of leading "b" characters in the joined string, which might be a defensive measure to handle potentially malformed input.
  2. Behavior with Valid Input:

    • For valid binary strings without leading "b" characters, both the original and modified codes should produce the same result.
    • If the binary string might sometimes have leading "b" characters, the modified code ensures that these are stripped before conversion.

Conclusion

The change appears to be a defensive improvement to handle potentially malformed input strings that might start with "b". This ensures that the conversion to an integer is robust and does not fail due to unexpected leading characters.

Recommendations

  • Unit Tests: Ensure there are unit tests covering cases with and without leading "b" characters to verify the correctness of the change.
  • Code Review: Have other maintainers or contributors review the change to confirm that it aligns with the intended behavior and does not introduce any regressions.

Given the above analysis, the change seems reasonable and potentially beneficial, especially if there have been issues with leading "b" characters in the past.

@Furtif Furtif merged commit 8f0a9cf into cedricp:master Mar 13, 2025
1 check passed
@Jodaille Jodaille deleted the fix-convert-bin branch March 13, 2025 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants