Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.008 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'encoding': 0.05; 'string': 0.09; 'subject:string': 0.09; 'subject:using': 0.09; 'subject:How': 0.10; 'cc:addr:python-list': 0.11; 'python': 0.11; 'assume': 0.14; 'literal.': 0.16; 'subject:variable': 0.16; '(you': 0.16; 'wrote:': 0.18; 'variable': 0.18; 'aug': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; 'bytes': 0.24; 'char': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'characters': 0.30; 'message-id:@mail.gmail.com': 0.30; 'convert': 0.35; 'received:google.com': 0.35; 'subject:?': 0.36; 'hi,': 0.36; 'e.g.': 0.38; 'pm,': 0.38; 'rather': 0.38; 'how': 0.40; 'length': 0.61; 'course': 0.61; '8bit%:10': 0.64; 'to:addr:gmail.com': 0.65; 'here)': 0.84; 'subject:pack': 0.84; 'whereas': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=orozco.fr; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=TEwGN8on3Es852/jq8niMgOeSl63GYf0xhBWUmFbC40=; b=jTVky5JJIgW2NqJYuBFNE3G9vFU4Vld+p9NhbaV61Z0dlt+jEkk0roLuNS3ANG71jG Oc4sz+uZWmDQgsSKSYtn/yGjK2xPODn9h7HcEpnQJFNkDhpDN1H2b/90YWPuHmbiKYe1 BamWa3qmHgIshrareg9g/Q/DCg/7p+FknWZLY= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=TEwGN8on3Es852/jq8niMgOeSl63GYf0xhBWUmFbC40=; b=cOB0FXwM3gDVir9nxR+ypwXt5z09riB39LET48SQEzXNpO7aplYzhiJagADE2vccs8 A/BsRvahiss6IDGuh1RlNlRlxtOHTcraOybUkc/0Tnq9WBC1BoRmurknkmZsyS1Nkl9k UtG3Is6Jxpdb/wbetcapJJVO9yfY5s6ExDRgJPFsg8grns7WmlsarQyQzJQEbwm9TSVG dhHd2XUBBB/ZFr/+/r7F/KWTdp4Vw6YM4nf0QDc5/TrSCVPt2WFysxM4oZ7vBTilmCTB o28W7TGzvCDlcyCdzOsOgA0D4kKhaZRU8f0gyNJYOsrSujSAjg1mOLIF7EgNM6W2OHo8 BgSg== X-Gm-Message-State: ALoCoQmBf5m8DpENB8RLvsEfPGOwNrbirtCjU6nGqXivKKn6BHPSOh7pWziS8C1ROA1uKFf2XT5M X-Received: by 10.182.47.195 with SMTP id f3mr5006849obn.61.1407241845042; Tue, 05 Aug 2014 05:30:45 -0700 (PDT) MIME-Version: 1.0 X-Originating-IP: [82.225.245.3] In-Reply-To: <64a5643f-a144-4292-9969-9f1743c40ad7@googlegroups.com> References: <64a5643f-a144-4292-9969-9f1743c40ad7@googlegroups.com> From: Thomas Orozco Date: Tue, 5 Aug 2014 14:30:24 +0200 Subject: Re: How to pack a string variable of length 1 as a char using struct.pack? To: danwgrace@gmail.com Content-Type: multipart/alternative; boundary=089e0158afca4983bb04ffe10641 X-Mailman-Approved-At: Tue, 05 Aug 2014 14:39:37 +0200 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 65 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407242377 news.xs4all.nl 2966 [2001:888:2000:d::a6]:46014 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75733 --089e0158afca4983bb04ffe10641 Content-Type: text/plain; charset=UTF-8 On Tue, Aug 5, 2014 at 2:15 PM, wrote: > Hi, > How to pack a string variable of length 1 as a char using struct.pack? > The following works fine: > p = struct.pack('c', b'1') > > Whereas this causes an error "char format requires a bytes object of > length 1": > s = '1' > p = struct.pack('c', s) > > I need to pack a variable rather than a literal. > I assume you are using Python 3. In Python 3, s = '1' is a *unicode string*, not a *bytes object*. You need to convert your string to a bytes object by encoding it. However, be mindful that some characters may actually require multiple bytes to be encoded: struct.pack('c', s.encode('ascii')) (You can of course use e.g. 'utf-8' as the encoding here) --089e0158afca4983bb04ffe10641 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On T= ue, Aug 5, 2014 at 2:15 PM, <danwgrace@gmail.com> wrote:<= br>
Hi,
How to pack a string variable of length 1 as a char using struct.pack?
The following works fine:
p =3D struct.pack('c', b'1')

Whereas this causes an error "char format requires a bytes object of l= ength 1":
s =3D '1'
p =3D struct.pack('c', s)

I need to pack a variable rather than a literal.

<= /div>I assume you are using Python 3. In Python 3, s =3D '1' is a *= unicode string*, not a *bytes object*.

You need to conve= rt your string to a bytes object by encoding it.=C2=A0

However, be mindful that some characters may actually r= equire multiple bytes to be encoded:

=C2=A0 =C2=A0= struct.pack('c', s.encode('ascii'))=C2=A0

(You can of course use e.g. 'utf-8' as the encoding here)<= /div>
--089e0158afca4983bb04ffe10641--