[("{'chip_id': '00012345', 'chec..." /> [("{'chip_id': '00012345', 'chec..." /> [("{'chip_id': '00012345', 'chec..."/>

Convert a string to hexadecimal in Python

1.4k Views Asked by At

Here is my function who build the message from an array. There are two type of checks:

  • Check "C", "G", "A" -> [("{'chip_id': '00012345', 'check_type': 'C'}", 1494273855.0)]

  • Check "P" -> ["{'latitude': 43.5529109, 'longitude': 1.4910036, 'check-type': 'P'}", 1494273855.0]

    def build(self, checks):
    #checks is an array.
    # #1- On transforme notre check en tuple
    _tuple = checks[0]
    #2- On recupere le couple id/type en string. On recupere le timestamp en string
    _type = _tuple[0]
    _timestamp = _tuple[1]
    #Selection taille message d apres le type element
    e = _type.find("type': '")
    type = _type[e+8]
    if type == "C" or type == "A" or type == "G":
        start = _type.find("'chip_id': '")
        stop = _type.find("', '")
        chip_id = _type[start + 12:stop]
        a = int(binascii.hexlify(chip_id))
        msg_build = bytes(type) + hex(int(_timestamp)) + hex(struct.unpack('<I', struct.pack('<f', a))[0]) + bytes(b"P")
    if type == "P":
        start_lat = _type.find("'latitude': ")
        end_lat =  _type.find(", 'long")
        latitude = float(_type[start_lat+12:end_lat])
        start_long = _type.find("'longitude': ")
        end_long = _type.find(", 'chec")
        longitude = float(_type[start_long+13:end_long])
        msg_build = bytes(type) + hex(int(_timestamp)) + hex(struct.unpack('<I', struct.pack('<f', latitude))[0]) +  hex(struct.unpack('<I', struct.pack('<f', longitude))[0])
    return msg_build

With this function, I can transform any checks into the desired message. It seems to work pretty well.

"C" check_type return message like "C0x5910e6b80x592c40b7P".

"P" check_type return message like "P0x5910ca4b0x420f12d00x428fca07"

But, in an other way, I will need to decrypt these message and get all informations. I can do it for my "P" messages.

For the check_type "C" I have some problem when I need to decrypt.

Let's take an example, I'll build the following check:

[("{'chip_id': '00014876', 'check_type': 'C'}", 1494279864.0)]

Where "chip_id" is always 8 digit, "check_type" will be "C", "G" or "A" (it doesn't really matter here) and the timestamp.

My function return:

C0x5910e6b80x592c40b7P

  • C is my type.
  • 0x5910e6b8 is my timestamp. If I do "int("0x5910e6b8", 0)", I find "1494279864" the timestamp in the check
  • 0x592c40b7 is my chip_id.

That's were my issue is. I could encrypt my chip_id with:

  • a = int(binascii.hexlify(chip_id))
  • hex(struct.unpack('<I', struct.pack('<f', a))[0])

But, I can't find how to get my chip_id ("00012345") from the hexadecimal(" 0x592c40b7") in the encrypted message.

Does someone know how I could do this?

1

There are 1 best solutions below

0
On

Does this fit the bill:

msg = ''.join(["{:02X}".format(e) for e in bytearray("Hello, world!")])
''.join([chr(int(msg[i:i+2], 16)) for i in range(0, len(msg), 2)])

?