Sending hex number as is

Antoon Pardon apardon at forel.vub.ac.be
Thu Mar 24 08:56:09 CET 2005


Op 2005-03-21, knguyen at megisto.com schreef <knguyen at megisto.com>:
> This question may be ased before, but I couldn't find the answer
> searching the archive.
>
> Basically, I just want to send a hex number from one machine to the
> next:

Hex numbers don't exist. You have just numbers. Those numbers can
be represented in different ways, but that doesn't make it a different
(kind of) number.

> for example
>
> msg = "Length is "
> n = '\x81'
> msg += n
> sock.send(msg)

You are not sending a number, you are sending a string. What you
seem to want is to represent this number in hexadecimal format
on this machine within a string and send that string to an other
machine.

> 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?

Use string formatting:

  msg = "Length is 0x%x" % n

-- 
Antoon Pardon



More information about the Python-list mailing list