简体   繁体   English

在Python中将字符串转换为十六进制

[英]Convert a string to hexadecimal in Python

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)] 检查“ C”,“ G”,“ A”-> [(“ {'{'chip_id':'00012345','check_type':'C'}”,1494273855.0)]

  • Check "P" -> ["{'latitude': 43.5529109, 'longitude': 1.4910036, 'check-type': 'P'}", 1494273855.0] 选中“ 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". “ C” check_type返回消息,例如“ C0x5910e6b80x592c40b7P”。

"P" check_type return message like "P0x5910ca4b0x420f12d00x428fca07" “ P” check_type返回消息,例如“ 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. 我可以针对我的“ P”邮件执行此操作。

For the check_type "C" I have some problem when I need to decrypt. 对于check_type“ C”,当我需要解密时会遇到一些问题。

Let's take an example, I'll build the following check: 让我们举个例子,我将建立以下检查:

[("{'chip_id': '00014876', 'check_type': 'C'}", 1494279864.0)] [(“ {{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. 在“ chip_id”始终为8位数的情况下,“ check_type”将为“ C”,“ G”或“ A”(在这里并不重要)和时间戳。

My function return: 我的函数返回:

C0x5910e6b80x592c40b7P C0x5910e6b80x592c40b7P

  • C is my type. C是我的类型。
  • 0x5910e6b8 is my timestamp. 0x5910e6b8是我的时间戳。 If I do "int("0x5910e6b8", 0)", I find "1494279864" the timestamp in the check 如果我执行“ int(“ 0x5910e6b8”,0)“,则在检查中找到时间戳记“ 1494279864”
  • 0x592c40b7 is my chip_id. 0x592c40b7是我的chip_id。

That's were my issue is. 那是我的问题。 I could encrypt my chip_id with: 我可以用以下方式加密我的chip_id:

  • a = int(binascii.hexlify(chip_id)) 一个= 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. 但是,我找不到如何从加密消息中的十六进制(“ 0x592c40b7”)中获取我的chip_id(“ 00012345”)。

Does someone know how I could do this? 有人知道我该怎么做吗?

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

?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM