首页 > 解决方案 > 我怎样才能摆脱这些“\xb0”之类的字符串

问题描述

我尝试使用 python 和 dll 从内存中转换一些数据。

但是有一些“unicode”的东西让我疯狂地使用结构来“转换”数据。

我试过decode('utf-8')但我面对UnicodeDecodeError

我改变了一些结构类型,但没有任何改变。

我该如何解决这个问题?

class NH0__OutBlock(Structure):
    _fields_ = [
        ("hotime", c_char*6),
        ("hotime1", c_char*6),
        ("offerho1", c_char*6),
        ("bidho1", c_char*6),
        ("offerrem1", c_char*6),
        ("bidrem1", c_char*6),
        ("offercnt1", c_char*5),
        ("bidcnt1", c_char*5),
        ("offerho2", c_char*6),
        ("bidho2", c_char*6),
        ("offerrem2", c_char*6),
        ("bidrem2", c_char*6),
        ("offercnt2", c_char*5),
        ("bidcnt2", c_char*5),
        ("offerho3", c_char*6),
        ("bidho3", c_char*6),
        ("offerrem3", c_char*6),
        ("bidrem3", c_char*6),
        ("offercnt3", c_char*5),
        ("bidcnt3", c_char*5),
        ("offerho4", c_char*6),
        ("bidho4", c_char*6),
        ("offerrem4", c_char*6),
        ("bidrem4", c_char*6),
        ("offercnt4", c_char*5),
        ("bidcnt4", c_char*5),
        ("offerho5", c_char*6),
        ("bidho5", c_char*6),
        ("offerrem5", c_char*6),
        ("bidrem5", c_char*6),
        ("offercnt5", c_char*5),
        ("bidcnt5", c_char*5),
        ("totofferrem", c_char*6),
        ("totbidrem", c_char*6),
        ("totoffercnt", c_char*5),
        ("totbidcnt", c_char*5),
        ("futcode", c_char*8)
    ]
...
# RealMsgPacket.contents.pszData Type is ctypes.c_char_p
elif RealMsgPacket.contents.szTrCode.decode('euc-kr') == "NH0":
            Casted = cast(RealMsgPacket.contents.pszData, POINTER(c_char*int(RealMsgPacket.contents.nDataLength)))
            print(Casted.contents.value.strip())
...
b'035822 275822 028345\xb0028340\xb0000012 000019 00004 00016 028350\xb0028335\xb0000014 000025 00007 00022 028355\xb002
8330\xb0000007 000020 00004 00015 028360\xb0028325\xb0000007 000022 00005 00011 028365\xb0028320\xc0000010 000037 00010
00015 000050 000123 00030 00079 101PC000'

标签: pythoncastingctypes

解决方案


推荐阅读