Re: Sending hex number as is

2005-03-24 Thread Antoon Pardon
Op 2005-03-21, [EMAIL PROTECTED] schreef [EMAIL PROTECTED]:
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sending hex number as is

2005-03-21 Thread Irmen de Jong
[EMAIL PROTECTED] wrote:
 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:
 
 for example
 
 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?
 
 Thanks,
 Khoa
 

Just send the number as a string?
(and parse it to an int again on the other side)

Or investigate the struct and/or pickle modules.

Alternatively, depending on the broader scope of what
you are doing, investigate a proper IPC library such
as Pyro.


--Irmen
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sending hex number as is

2005-03-21 Thread Grant Edwards
On 2005-03-21, [EMAIL PROTECTED] [EMAIL PROTECTED] 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.coman AGENT!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Sending hex number as is

2005-03-21 Thread Terry Reedy

[EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 Basically, I just want to send a hex number from one machine to the
 next:

'Hex number' is not completely clear.  Do you want to send the number in 
binary form or in text form.  Text form is much easier and more dependable, 
so should be prefered unless you have a good reason for binary.  When 
sending a text string, you still have a choice between decimal and other 
representations.  Again, decimal is easier and more dependable, and 
therefore prefered, unless the receiving end really demands otherwise.

msg = 'The length is %s' % len(something)

Terry J. Reedy



-- 
http://mail.python.org/mailman/listinfo/python-list