By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,404 Members | 1,074 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,404 IT Pros & Developers. It's quick & easy.

Sending hex number as is

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
kn*****@megisto.com 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
Jul 18 '05 #2

P: n/a
On 2005-03-21, kn*****@megisto.com <kn*****@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!!
Jul 18 '05 #3

P: n/a

<kn*****@megisto.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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

Jul 18 '05 #4

P: n/a
Op 2005-03-21, kn*****@megisto.com schreef <kn*****@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
Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.