Subject | pack problem in Python 3 |
---|---|
Author | Philippe Makowski |
Post date | 2012-01-12T13:46:19Z |
Hi,
I need your help
this piece of code have no problem under Python2
#coding:utf-8
#
import struct
def addString(codeAsByte, s):
sLen = len(s)
format = 'cc%ds' % sLen # like 'cc50s' for a 50-byte string
newEntry = struct.pack(format, chr(codeAsByte), chr(sLen), s)
def addInt(codeAsByte, value):
newEntry = struct.pack('ccc', chr(codeAsByte), '\x01', chr(value))
if __name__=='__main__':
addInt(10,10)
addString(28,'test'.encode("utf8"))
but under Python3 I get
struct.error: char format requires a bytes object of length 1
for both
any clue ?
I need your help
this piece of code have no problem under Python2
#coding:utf-8
#
import struct
def addString(codeAsByte, s):
sLen = len(s)
format = 'cc%ds' % sLen # like 'cc50s' for a 50-byte string
newEntry = struct.pack(format, chr(codeAsByte), chr(sLen), s)
def addInt(codeAsByte, value):
newEntry = struct.pack('ccc', chr(codeAsByte), '\x01', chr(value))
if __name__=='__main__':
addInt(10,10)
addString(28,'test'.encode("utf8"))
but under Python3 I get
struct.error: char format requires a bytes object of length 1
for both
any clue ?