0

I need to send a byte string like this one to a UDP socket

request=b'\x00\x01\x02\xc3\x68\x44\xcc\x61\x0e\x05\x05'

This is a connection request, and I am able to create the connection,

The third byte \x02 is a tag number that I would like to change from the code, so I did the following

request = b'\x00\x01' + tag.to_bytes(1,'little') + b'\x02\xc3\x68\x44\xcc\x61\x0e\05\05'

where tag is the variable that stores my tag number

When I send this request to the socket it does not work. When printing the request the reason is clear: it has changed. In the above example, assuming that tag == 2,

>>> request = b'\x00\x01' + number.to_bytes(1,'little') + b'\x02\xc3\x68\x44\xcc\x61\x0e\05\05'
>>> print(request)
b'\x00\x01\x02\x02\xc3hD\xcca\x0e\x05\x05'

while what I was expecting was

request=b'\x00\x01\x02\xc3\x68\x44\xcc\x61\x0e\05\05'

It seems that other people has had similar problems, String to Bytes Python without change in encoding attributing the problem to the way the print() function represents the string. In my case, if I send the byte string that includes the tag variable, I am unable to create a connection, so looks like the underlying code in the socket library behaves similarly to the print() function.

I have also tried How to create python bytes object from long hex string? with no success. I can put together a hex string, but then I get the same problem as before

>>> print(bytes.fromhex('000102c36844cc610e0505'))
b'\x00\x01\x02\xc3hD\xcca\x0e\x05\x05'

How can I get a bytes string representation of the string \x00\x01\x02\xc3hD\xcca\x0e\x05\x05?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • There are some good answers & comments covering this here. [link](https://stackoverflow.com/questions/34690025/print-bytes-to-hex). the answer from @jfs covers how to print your hex as a string and the reasons behind why it displays differently. – PacketLoss Jul 28 '20 at 00:27
  • 1
    If you want to _remove_ the third byte (`\x02`), why are you including it in the part after `tag.to_bytes(1,'little')`? – zvone Jul 28 '20 at 00:52
  • BTW, `b'\x00\x01\x02\xc3\x68\x44\xcc\x61\x0e\x05\x05'` is the same thing as `b'\x00\x01\x02\xc3hD\xcca\x0e\x05\x05'`. Try `print(b'\x00\x01\x02\xc3\x68\x44\xcc\x61\x0e\x05\x05')` and see for yourself. – zvone Jul 28 '20 at 00:53
  • python for unprintable values display hex codes but for printable values it prints its chars and `print(b'\x68\x44')` gives `b'hD'` - but there are the same bytes and `b'\x68\x44' == b'hD'` gives `True`. And `b'hD'[0], b'\x68\x44'[0]` gives the same values `(104,104)`. All your problem can be only in `tag` value. Maybe it has to be converted in different way. Maybe it has to be char `"2"` (`'x32'` - `hex(ord('2'))`) instead of number `2` (and `\x02`) – furas Jul 28 '20 at 02:38
  • Thanks to all of you that replied. At the end, when I wrote _When I send this request to the socket it does not work_ I was wrong (I was making an error elsewhere). It works. What means, as @zvone pointed, that b'\x00\x01\x02\xc3\x68\x44\xcc\x61\x0e\x05\x05' and b'\x00\x01\x02\xc3hD\xcca\x0e\x05\x05'are the same thing. I still do not understand why python represents \xc3\x68\x44\xcc like xc3hD\xcca when using the print() function. – Carles Roch-Cunill Aug 25 '20 at 16:31
  • @CarlesRoch-Cunill Python represents all printable ascii characters as the ascii characters and only shows the others as hex, because that is shorter and easier to understand. You would not want `b'ABCDE'` to show up as `b'\x41\x42\x43\x44\x45'`, would you? – zvone Aug 25 '20 at 18:38
  • Thanks @zvone for your clarification. However, in this case, yes!, I wanted to send `b'\x41\x42\x43\x44\x45'` as this is what the protocol (RTP) is expecting. My original problem was the RTP packets I was creating didn't contain the information I sent. To debug I print() the request and is when I found the representation changed. At the end I found that seems to be only print() that changes the representation. The socket accepts correctly the characters I am sending. I had made a mistake and I though the socket was also changing the representation the same as print(). – Carles Roch-Cunill Aug 26 '20 at 20:27

0 Answers0