0

So I am connecting to a IoT machine.

I have a bytestring

my_string = '\x01\x00\t\xd2\x01\x00\x01\x04keso\xcc' 

Sending the string like this works perfect:

self._sock.send(b'\x01\x00\t\xd2\x01\x00\x01\x04keso\xcc') 

but this fails

self._sock.send(my_string) #=> Error #TypeError: a bytes-like object is required, not 'str' 

so, my mission now is to get the string to be sent, and when i try to add my_string.encode(), then string will become b'\x01\x00\t\xc3\x92\x01\x00\x01\x04keso\xc3\x8c' which is not a correct representation, should be b'\x01\x00\t\xd2\x01\x00\x01\x04keso\xcc'

How the heck do I convert this?

3
  • my_string is not a byte string. Commented Sep 6, 2019 at 21:39
  • Ok, but regardless of that, it is what I have to work with. I cannot change that, its the only type of string that the iot device takes. Commented Sep 6, 2019 at 23:34
  • What? What do you mean it's the only type of string that the IOT device takes. What is creating this string? Commented Sep 6, 2019 at 23:46

2 Answers 2

1

I have found it. It was using latin1 encoding.

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

Comments

0
my_string = '\x01\x00\t\xd2\x01\x00\x01\x04keso\xcc' 

...is not a bytestring. I think it should be ...

my_string = b'\x01\x00\t\xd2\x01\x00\x01\x04keso\xcc' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.