0

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?

1 Answer 1

1

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.

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

3 Comments

Thanks your answer,but if my string is a variable , just like my_str = '232335383931' prepare(b''.join(my_str)) # tips expect byte , string found
@gassy either do my_str = b'232335383931' OR prepare(my_str.encode()) (don't do both!)
hi, but how to get b'\x23\x23\x35\x38\x39\x31' , I used b'\x' + string[i:i + 2] , it error, and use b'\\x' + string[i:i + 2] , the result is b'\\x23\\x23\\x35\\x38\\x39\\x31' ,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.