Re: [問題] python有類似指標的操作嗎?

看板Programming作者 (殘雲奪月)時間14年前 (2011/07/07 00:03), 編輯推噓1(103)
留言4則, 2人參與, 最新討論串2/3 (看更多)
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

07/07 10:20, , 1F
thx 但其實我也需要取2byte 跟4byte
07/07 10:20, 1F

07/07 11:53, , 2F
arr1=array('b', arr.tostring())#1byte
07/07 11:53, 2F

07/07 12:00, , 3F
arr4=array('i', arr.tostring())#4byte
07/07 12:00, 3F

07/07 12:01, , 4F
arr2=array('h', arr.tostring())#2byte
07/07 12:01, 4F
※ 編輯: ya790206 來自: 175.180.100.193 (07/07 18:21) ※ 編輯: ya790206 來自: 175.180.100.193 (07/07 18:22)
文章代碼(AID): #1E58TLjK (Programming)
文章代碼(AID): #1E58TLjK (Programming)