作者ya790206 (殘雲奪月)
看板Programming
標題Re: [問題] python有類似指標的操作嗎?
時間Thu Jul 7 00:03:30 2011
C的寫法:
int a[10] = {0,1,2,3,4,5,6,7,8,9};
char *ptr8 = a;
ptr8[0],ptr8[1]....的取值動作
我不知道有沒其他更好的寫法
這是我自己想出來的方法
python的寫法:
from array import *
arr=array('i', [65,66,65])#前面的i表示signed int,
#實際大小由C implementation決定,其他符號看官方文件
ptr=arr.tostring()
print ord(ptr[0])#ord 把 字元轉成數字
print ord(ptr[1])
print ord(ptr[2])
print ord(ptr[3])
print ord(ptr[4])
arr1=array('b', arr.tostring())#一次讀1byte
arr2=array('h', arr.tostring())#一次讀2byte
arr4=array('i', arr.tostring())#一次讀4byte
希望我沒誤解你的意思
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 175.180.100.193
推 final01:thx 但其實我也需要取2byte 跟4byte140.118.122.154 07/07 10:20
→ ya790206:arr1=array('b', arr.tostring())#1byte175.180.100.193 07/07 11:53
→ ya790206:arr4=array('i', arr.tostring())#4byte175.180.100.193 07/07 12:00
→ ya790206:arr2=array('h', arr.tostring())#2byte175.180.100.193 07/07 12:01
※ 編輯: ya790206 來自: 175.180.100.193 (07/07 18:21)
※ 編輯: ya790206 來自: 175.180.100.193 (07/07 18:22)