Home > OS >  How to send String as hexadecimal in python socket?
How to send String as hexadecimal in python socket?

Time:11-06

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

b'0x230x230x350x380x390x31'?

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?

CodePudding user response:

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)
    )


print(prepare(b'232335383931'))

Output:

b'0x230x230x350x380x390x31'

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

  • Related