0

fast question, I want this (of course python3):

frame = "AAA555AAA555AAA555AAA555AAA555AAA555AAA555AAA555AAA555AAA555AAA"

to this:

frame = [0xA, 0xA, 0xA, 0x5, 0x5, 0x5, 0xA, 0xA, 0xA, 0x5, 0x5, 0x5, 0xA, 0xA, 0xA, 0x5, 0x5, 0x5, 0xA, 0xA, 0xA, 0x5, 0x5, 0x5, 0xA, 0xA, 0xA, 0x5, 0x5, 0x5, 0xA, 0xA, 0xA, 0x5, 0x5, 0x5, 0xA, 0xA, 0xA, 0x5, 0x5, 0x5, 0xA, 0xA, 0xA, 0x5, 0x5, 0x5, 0xA, 0xA, 0xA, 0x5, 0x5, 0x5, 0xA, 0xA, 0xA, 0x5, 0x5, 0x5, 0xA, 0xA, 0xA]
2
  • 2
    0xA is actually the number 10. That's what you get when you write that. Are you okay with it? Or are you maybe looking for the string '0xA'? Commented Nov 29, 2020 at 18:40
  • what do you mean by a "hex array"? That is a list not an array, and those are int objects Commented Nov 29, 2020 at 18:44

1 Answer 1

1

We can try this:

frame = "AAA555AAA555AAA555AAA555AAA555AAA555AAA555AAA555AAA555AAA555AAA"
[int(character, 16) for character in frame]
# [10, 10, 10, 5, 5, 5, 10, 10, 10, 5, ... ]

As @Reti43 notes in a comment, hexadecimal values like 0xA correspond to numbers like 10, so that's why the resulting list has integers.

If the OP desires hexadecimal, string representation can be used as below. This string can then be passed to int to get the corresponding integer.

frame = "AAA555AAA555AAA555AAA555AAA555AAA555AAA555AAA555AAA555AAA555AAA"
[f"0x{character}" for character in frame]
# ['0xA', '0xA', '0xA', '0x5', '0x5', '0x5', '0xA', '0xA', '0xA', '0x5', ...]
Sign up to request clarification or add additional context in comments.

2 Comments

i just realized that int array is the same as hex array, only difference is in representation :)
The '0x' prefix isn't necessary since you have to declare the base when calling int() anyway. [int(c, 16) for c in frame] will suffice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.