Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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


print(prepare(b'232335383931'))

Output:

b'0x230x230x350x380x390x31'

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading