470,836 Members | 1,880 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,836 developers. It's quick & easy.

packing hex value for socket.send

hi all,

i've been working on a communication server to talk with an io controller
device. i've written a quick and dirty php script a while ago and now
i need to port it to python (for a real comm. server program). but i
couldn't achieve to send commands over opened socket connection by converting
them to raw format using pack command.

the working php function looks like:

// {{{ php code
$commandList = array('01', '01', '00', '00', '00', '08', '3D', 'CC');
foreach ( $commandList as $hexnum ) {
/*
The mean of 'H*' differs from the one in pythons pack command.
I pasted below explanation from php documentation:
i. The repeater argument can be either an integer value or * for
repeating to the end of the input data.
ii. Code: H
Description: Hex string, high nibble first.
*/
$buf = pack('H*', $hexnum);
socket_write($socket, $buf, strlen($buf));
}
// }}}

I've tried below commands in python, but they didn't work:

# {{{ python code
"""
i know command list differs from the below one but
that doesn't matter. because there's already dosens
of these ones.
"""
cmdList1 = ("01", "05", "00", "01", "FF", "FF", "9D", "BA")
cmdList2 = (0x01, 0x05, 0x00, 0x01, 0xFF, 0xFF, 0x9D, 0xBA)

for command in cmdList1:
code = pack("cc", command)
channel.send(code)

for command in cmdList2:
"""
i'm converting hex command to integer, because
pack command doesn't recognize hex values.
"""
command = int(command)
code = pack("I", command)
channel.send(code)
# }}}

There doesn't exist a `H' code and `*' param. in pythons pack function (please
correct if i'm wrong), so i stucked in here. I'd be so appreciated if somebody
could help to fix that problem.

best regards

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA8j6NGcWq5WdHoacRAiZAAJ931lw3psZIRlYjaa5VNW AVJDBLpACgoPCl
EbhXbOTcU8Z1jVgW8MSSTd4=
=o7Rm
-----END PGP SIGNATURE-----

Jul 18 '05 #1
1 4637
Volkan YAZICI <ya******@itu.edu.tr> writes:
There doesn't exist a `H' code and `*' param. in pythons pack
function (please correct if i'm wrong), so i stucked in here. I'd be
so appreciated if somebody could help to fix that problem.


See the docs for the binascii module. You want the binascii.unhexlify
function, I think.
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Hiroyuki Tanaka | last post: by
2 posts views Thread by Matt | last post: by
1 post views Thread by yaron | last post: by
1 post views Thread by CKane | last post: by
3 posts views Thread by EricKit | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.