0

So I get a list of mentions from twitter, and need to know the index of each mention. To do this I use:

for idx, result in enumerate(mentions, start = 48):
message = result['text']
print idx, message

which returns

48 " first message" 

as expected. However, I need to use this index for some serial data that requires me to convert the index into hex. So.. I use:

hidx = hex(idx)

which then returns 0x30. However I need to somehow have this result in the exact format of "\x30" so that I can use serial to write:

serial.write("\x30")

what is the best way to accomplish this? If I keep my hex-converted index code the way it is, I get that pesky extra 0 and no backslash that causes the serial code to actually write serial.write(0x30), which is not what I need. Im hoping to find a way that, because of the for loop, I will receive:

serial.write("\x30")
serial.write("\x31")
serial.write("\x32")

ect. for as many mentions as I need. Is there a way to strip the first zero and add the \? Maybe a better way? Im new to python and serial communication so any help will be much appreciated.

1 Answer 1

1

(Assuming Python 2) "\x30" is a 1-byte string and the byte in question is the one at ASCII code 48:

>>> print repr('\x30')
'0'

So, all you need to do to emit it is serial.write(chr(idx)) -- no need to mess with hex!

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

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.