Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75730

How to pack a string variable of length 1 as a char using struct.pack?

Newsgroups comp.lang.python
Date 2014-08-05 05:15 -0700
Message-ID <64a5643f-a144-4292-9969-9f1743c40ad7@googlegroups.com> (permalink)
Subject How to pack a string variable of length 1 as a char using struct.pack?
From danwgrace@gmail.com

Show all headers | View raw


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.

Thanks.

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

How to pack a string variable of length 1 as a char using struct.pack? danwgrace@gmail.com - 2014-08-05 05:15 -0700
  Re: How to pack a string variable of length 1 as a char using struct.pack? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-08-05 22:28 +1000
  Re: How to pack a string variable of length 1 as a char using struct.pack? Thomas Orozco <thomas@orozco.fr> - 2014-08-05 14:30 +0200

csiph-web