第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

編輯 Ctypes 結(jié)構(gòu)的緩沖區(qū)

編輯 Ctypes 結(jié)構(gòu)的緩沖區(qū)

慕村225694 2022-06-28 11:09:55
我有一個(gè) Ctypes 結(jié)構(gòu),它需要以易于實(shí)現(xiàn)的標(biāo)準(zhǔn)方式 [setattr(structure,value)] 進(jìn)行編輯,但我還需要能夠編輯原始緩沖區(qū),因?yàn)槲蚁M軌蚍峙淠硞€(gè)位到一個(gè)值(例如位 25 = 0xd5) 我該怎么做?非常簡(jiǎn)化的示例代碼,如果有幫助的話import ctypes as *#ctypes array with ~250,000 c_uint32 elementshuge_arr = (c_uint32*250,000)(*range(250,000))  # Fill with dummy data for this exampleclass Example(Structure):    _pack_ = 1    _fields_ = [        ("a", c_uint16),        ("b", c_uint16, 14),        ("c", c_uint16, 2),        ("d", c_uint32, 24),        ("e", c_uint32, 8),        ("f", c_uint16),        ("g", c_uint16)    ]offset = 123456example_struct = Example.from_buffer(huge_arr, offset)# Ideally, I'd like to be able to set bits in example_struct. for example, example_struct[2] = 0x2b我知道可以通過執(zhí)行 huge_arr[offset+2] = 0x2b 來執(zhí)行 example_struct[2] = 0x2b,但我的程序比這個(gè)示例更復(fù)雜,huge_arr 被定義(并保留)在主文件中,而 example_struct 被傳輸作為不同文件中另一個(gè)函數(shù)的參數(shù),因此 huge_arr 超出范圍。有沒有辦法改變 exmaple_struct 的第 n 位?需要注意的另一件事是,該程序是用 Python 2.7 編寫的,但即使是 python3 解決方案也將不勝感激預(yù)先感謝您的幫助(我肯定會(huì)為任何可以解決此問題的善良的靈魂標(biāo)記最佳答案)
查看完整描述

1 回答

?
子衿沉夜

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

清單[Python 3.Docs]:ctypes - Python 的外部函數(shù)庫。


您在問題中的幾個(gè)地方使用了“位”一詞,但您的意思可能是“字節(jié)”(因?yàn)槲恢荒芫哂?或1的值)。


為了實(shí)現(xiàn)您的目標(biāo),您可以將結(jié)構(gòu)包裝在一個(gè)聯(lián)合中。


code00.py:


#!/usr/bin/env python3


import sys

import ctypes as ct



class ExampleStruct(ct.Structure):

    _pack_ = 1

    _fields_ = [

        ("a", ct.c_uint16),

        ("b", ct.c_uint16, 14),

        ("c", ct.c_uint16, 2),

        ("d", ct.c_uint32, 24),

        ("e", ct.c_uint32, 8),  # @TODO - cfati: Why not c_uint8 ???

        ("f", ct.c_uint16),

        ("g", ct.c_uint16),

        ("test_field", ct.c_uint8),  # One byte field would make the example more eloquent

    ]



class Example(ct.Union):

    _anonymous_ = ["struct"]

    _fields_ = [

        ("struct", ExampleStruct),

        ("raw", ct.c_ubyte * ct.sizeof(ExampleStruct)),

    ]



def main():

    huge_arr_size = 250000

    huge_arr = (ct.c_uint32 * huge_arr_size)(*range(huge_arr_size))

    arr_offset = 123456

    example = Example.from_buffer(huge_arr, arr_offset)

    print("example.test_field: {0:d}".format(example.test_field))

    test_field_offset = Example.test_field.offset

    example.raw[test_field_offset] = 123

    print("example.test_field: {0:d}".format(example.test_field))



if __name__ == "__main__":

    print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))

    main()

    print("\nDone.")

輸出:


[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q058460001]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" code00.py

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 64bit on win32


example.test_field: 147

example.test_field: 123


Done.


查看完整回答
反對(duì) 回復(fù) 2022-06-28
  • 1 回答
  • 0 關(guān)注
  • 118 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)