How to send String as hexadecimal in python socket?

If I want to use Python to send hexadecimal data to the Java code on the server for parsing, is this the only option:


How to send it in a simple form like B '0x232335383931'? Because I need to change the numbers frequently, I have tried it by myself, but I can’t. Is there any other solution?

>Solution :

It looks like this is a problem not in Python but with the server you are sending it to: that server is the thing doing the parsing. Here’s one way to deal with it:

def prepare(string):
    return b''.join(
        b'0x' + string[i:i + 2]
        for i in range(0, len(string), 2)




You can easily edit b'232335383931', then prepare it before sending it to your Java server.

Leave a Reply