GO GO GO, Fire in the Hole!!!

Thursday, December 22, 2005

 

封装struct的ByteArray的实现

最近因项目需要要在Python里面使用ByteArray,于是用struct封装了一下:

import struct
class ByteArray:
"""
A class to simulate ByteArray.
@author tyraeltong@gmail.com
@attention call writexxx(),last call getContent() to get result.
"""
m_cont=[]
def writeByte(self, b):
self.m_cont.append(struct.pack('B', b))
def writeShort(self, s):
self.m_cont.append(struct.pack('H', s))
def writeInt(self, i):
self.m_cont.append(struct.pack('I', i))
def writeString(self, s):
self.m_cont.append(s)
def getContent(self):
return ''.join(self.m_cont)

奇怪的是struct的's'format,好像对字符串并不起作用,下面的语句:
struct.pack('s', 'abcd')
只会返回 'a'

Comments:
你是一月“勃”一次,一次“勃”一月……
 
你晓得撒,我不喜欢写日记的
 
Post a Comment





<< Home

Archives

October 2005   December 2005   January 2006   June 2009  

This page is powered by Blogger. Isn't yours?

Subscribe to Posts [Atom]