Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't convert to HEX - TypeError: Non-hexadecimal digit found

The line deviceToken = deviceToken.replace(' ','').decode('hex') is crashing my script. this is the device token: (with a few numbers changed)

deviceToken = '9cdcb815 d93e11ce 52baaf6c 14e27cc8 31d5ce62 2e51ce6d f75692c2 3617cadb'

The first push notification is sending out fine so I'm sure the device token is ok but after the first event I'm getting this error:

['"INBOX" (UNSEEN 12)'] 
Sent Push alert.
Got an event!
['"INBOX" (UNSEEN 13)']
Exception in thread Thread-4:Exception in thread Thread-4:
    Traceback (most recent call last):
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 530, in __bootstrap_inner
        self.run()
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/threading.py", line 483, in run
        self.__target(*self.__args, **self.__kwargs)
      File "server.py", line 111, in idle
        self.dosync()
      File "server.py", line 118, in dosync
        sendPushNotification(numUnseen)
      File "server.py", line 54, in sendPushNotification
        deviceToken = deviceToken.replace(' ','').decode('hex')
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/hex_codec.py", line 42, in hex_decode
        output = binascii.a2b_hex(input)
    TypeError: Non-hexadecimal digit found

Link to the script

like image 934
Segev Avatar asked Sep 17 '25 09:09

Segev


1 Answers

Your device token is probably not what you think it is. Perhaps it has a newline or some other character that you can't see.

As a simple test:

>>> deviceToken = '9cdcb815 d93e11ce 52baaf6c 14e27cc8 31d5ce62 2e51ce6d f75692c2 3617cadb'
>>> deviceToken.replace(' ','')
'9cdcb815d93e11ce52baaf6c14e27cc831d5ce622e51ce6df75692c23617cadb'
>>> deviceToken.replace(' ','').decode('hex')
'\x9c\xdc\xb8\x15\xd9>\x11\xceR\xba\xafl\x14\xe2|\xc81\xd5\xceb.Q\xcem\xf7V\x92\xc26\x17\xca\xdb'
like image 129
Ian Clelland Avatar answered Sep 19 '25 22:09

Ian Clelland