python - Replace values in a ctypes c_char array -


i have program creates array of 8192 bytes of data. want change value below returnbuffer array:

serialbuff = returnbuffer[0x14:0x28] datatowrite = "15510580029600000000"  returnbuffer = returnbuffer.replace(serialbuff, datatowrite) 

but result got :

attributeerror: 'c_char_array_8192' object has no attribute 'replace' 

can please me?

the error because arrays don't have replace method. know have no public facing methods.

in order replace contents either perform 1 1 assignement (no need write counters in hex, it's allowed why not):

for in range(0x14, 0x28):     returnbuffer[i] = datatowrite[i - 0x14] 

or use slicing (as suggested @eryksun):

returnbuffer[0x14: 0x28] = datatowrite.encode('utf-8') 

one of these should trick.

as example more simple values:

in [71]: arr = 5 * ctypes.c_char  in [72]: c_arr = arr("0", "1", "2", "3", "4")  in [73]: print c_arr[:] 01234  in [74]: replacevals = "321"  in [75]: in range(0x1, 0x4):     ...:     c_arr[i] = replacevals[i - 0x1]   in [76]: print c_arr[:] "03214" 

or, slicing:

in [77]: c_arr[0x1: 0x4] = raplce.encode('utf-8') in [78]: c_arr[:] "03214" 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -