0

I want to get the first 6 bytes from payload as a single number or string.

for byte_pos in range(6):
   byte_content  = ord(payload[byte_pos])

Assume the payload is 1 2 3 4 5 6,

for byte_pos in range(6):
   print ord(payload[byte_pos])

This will result as follows, 0x1 0x2 0x3 0x4 0x5 0x6

But what I need is a single number/string like 123456. How to combine these single numbers to make 123456?

How to convert these 6 byte_contents to a single number or string.

1
  • An example would be good. Commented Apr 24, 2019 at 8:19

3 Answers 3

2

If you are reading bytes, it means you are reading integers from 0 to 255. So you can turn those numbers quickly to base-10 like this : int(str(byte), 2)

If you want to turn bytes into characters, you might use the chr() function : char = chr(int(str(byte), 2))

Sign up to request clarification or add additional context in comments.

Comments

1

If you are working with Python 2.x here's an answer (if I understood what you want to do) :

payload  = bytearray(b'\x41\x42\x43') #Hex code for ABC
final_string = ''


for byte_pos in range(3):
   byte_content  = chr(payload[byte_pos])
   #print byte_content
   final_string = final_string + byte_content
   print final_string

Output will be :

A
AB
ABC

Comments

0
dst_mac = ''
for byte_pos in range(6):
    dst_mac = dst_mac + str(hex((ord(payload[byte_pos])))[2:])
print dst_mac

This way, it worked.

Thank you

Comments

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.