Sending hex number as is

Grant Edwards grante at visi.com
Mon Mar 21 15:47:22 EST 2005


On 2005-03-21, knguyen at megisto.com <knguyen at megisto.com> wrote:

> msg = "Length is "
> n = '\x81'
> msg += n
> sock.send(msg)
>
> The problem is n's value is not fixed. For example,
>
> msg = "Length is "
> n = len(somestring)
> msg += n  # This won't work of course, since n is int
>
> How do I send this msg + n?

n = 0x81
msg = "Length is " + chr(n)

That only works for "hex numbers" than can be properly encoded
as a character in the default encoding.

See the "struct" module for a more general solution.

-- 
Grant Edwards                   grante             Yow!  There's a lot of BIG
                                  at               MONEY in MISERY if you have
                               visi.com            an AGENT!!



More information about the Python-list mailing list