Sending hex number as is

K

knguyen

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
 
I

Irmen de Jong

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
 
G

Grant Edwards

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.
 
T

Terry Reedy

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
 
A

Antoon Pardon

Op 2005-03-21 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,228
Messages
2,571,157
Members
47,785
Latest member
deepusaini

Latest Threads

Top