Python Forum
enter string into list according to a known rule
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
enter string into list according to a known rule
#5
(Aug-11-2021, 02:06 PM)DeaD_EyE Wrote: The provided example could not work because the quoting is missing.

import binascii


from_your_post = "d8 59 80 a2 c8 be f4 92".replace(" ","")

# unhexlify can not handle whitespaces, so I've replaced them with nothing
raw_bytes = binascii.unhexlify(from_your_post)

# iterating over bytes -> you get integers (0-255, one Byte) back
# accessing an index on raw bytes also return an int
# consuming raw_bytes with a list, tuple or other sequence types, will also make integers
# but accessing a range of bytes, will still return raw bytes
# the bytes type in Python is a hybrid.

for value in raw_bytes:
    print(value, type(value))
To keep the values, you can append them to a list or consume the raw_bytes with a list:
integers = list(raw_bytes)
print(integers)
Output:
[216, 89, 128, 162, 200, 190, 244, 146]


I did somthing else , and now I have the wanted value were I want it
but when I try to convert it from Hex str to int I get error?
['ee', 'ad', 'd0', '76', 'dd', 'ff', '52', '00']
<class 'str'>
Traceback (most recent call last):
  File "Documents/GetVIN.py", line 61, in <module>
    x = int(TestValue, 16)
ValueError: invalid literal for int() with base 16: "['dd']"
"
this is what I have done:
def CutData(FullData, StartByte, length=1):
    StartByte = StartByte - 1
    EndByte = StartByte + length
    Data_Byte = FullData.split(' ')
    print(Data_Byte)  
    Wanted_Data = str(Data_Byte[StartByte:EndByte])
    print(type(Wanted_Data))
    return Wanted_Data

TestValue = (CutData(Data, 5, 1))
                x = int(TestValue, 16)
                print(x)
what is wrong?
I have str in hex and want to make it an int
is the command is wrong?
what am I missing?

Thanks,
Reply


Messages In This Thread
RE: enter string into list according to a known rule - by korenron - Aug-11-2021, 02:46 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Python rule about the space character surrounding the equal sign ineuw 10 1,643 Sep-21-2023, 09:17 AM
Last Post: ineuw
  Pythone module for dynamic rule execution hemal07yc 1 9,018 Sep-13-2019, 11:25 AM
Last Post: luoheng
  I converted string to 'list', but it doesn't look like a list! mrapple2020 3 3,263 Apr-07-2019, 02:34 PM
Last Post: mrapple2020
  Create Alert if string from list appears on other list javalava 1 2,526 Sep-17-2018, 02:44 PM
Last Post: DeaD_EyE
  List of pathlib.Paths Not Ordered As Same List of Same String Filenames QbLearningPython 20 15,445 Nov-16-2017, 04:47 PM
Last Post: QbLearningPython
  Create a new list by comparing values in a list and string DBS 2 3,542 Jan-14-2017, 07:59 AM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020