Decoding Hex char string in python
Question: I'm trying to interface a software with python via a TCP connection. The
software is AGWPE (in fact, it's soundmodem, but they share the same API).
When I ask the software for "normal" packets, everything fine, but when I ask
for "raw" packets, the hex bytes the tcp connection gives me won't decode as
nothing at all. Not ASCII, not unicode, not latin-1.

This is the API of AGWPE already to the packet I'm having trouble with:
<http://uz7.ho.ua/includes/agwpeapi.htm#_Toc500723814>

This is my code:

    
    
    import socket
    #import aprslib
    
    raw=1
    
    
    TCP_IP = '127.0.0.1'
    TCP_PORT = 8000
    BUFFER_SIZE =  1024
    MESSAGE = '000000006D00000000000000000000000000000000000000000000000000000000000000'
    message2 = '000000006B00000000000000000000000000000000000000000000000000000000000000'
    MESSAGE = bytearray.fromhex(MESSAGE)
    message2 = bytearray.fromhex(message2)
    
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((TCP_IP, TCP_PORT))
    s.send(MESSAGE)
    if raw:
        s.send(message2)
    
    data = s.recv(BUFFER_SIZE)
    dataf=data.decode("ascii", 'ignore') #not putting the second argument raises an exception
    #aprs=aprslib.parse(dataf)
    print(dataf)
    s.close()
    

And this is my result: (I would paste it but seems that notepad and the
browser don't like some characters) Raw on:
<http://i.gyazo.com/2e6eecf99c6e79c7cdbcf868a734694b.png>

I would be expecting something like on the example:

....K...LU7DID..8.LU7DID-4..B...,.E....n......n...i8...[LU7DID@LU7DID-4]
B,C,D,E,X,I,M,?,N,P,U,J,R: ..

with the char 0x00 instead of . of course. At least something readable.

Thanks a lot for your time.


Answer: I assume you're using python 3. Try something like:

    
    
    bytearray.fromhex(data).decode()
    

From [python
docs](https://docs.python.org/3/library/stdtypes.html#bytearray.fromhex)

