[Tutor] Conversion question

Tom Green xchimeras at gmail.com
Tue May 5 12:17:42 CEST 2009


Thanks everyone for your replies.  This reason for the long string is
sometimes I have to pass a 32 byte key (MD5 hash value) to the socket.  The
data I am sending is the hex values i.e. 41=A.  I believe the binascii will
work.

Mike

On Mon, May 4, 2009 at 7:52 PM, Alan Gauld <alan.gauld at btinternet.com>wrote:

>
> "Tom Green" <xchimeras at gmail.com> wrote
>
>  Here is my question.  I work with a lot of sockets and most of them
>> require
>> hex data.  I am usually given a string of data to send to the socket.
>> Example:
>>
>> "414243440d0a"
>>
>> Is there a way in Python to say this is a string of HEX characters like
>> Perl's pack?  Right now I have to take the string and add a \x to every
>> two
>> values i.e. \x41\x42...
>>
>
> Assuming you actually want to send the hex values rather than
> a hex string representation then the way I'd send that would be
> to convert that to a number using int() then transmit it using
> struct()
>
>  Sometimes my string values are 99+ bytes in length.  I did write a parsing
>> program that would basically loop thru the string and insert the \x, but I
>> was wondering if there was another or better way.
>>
>
> OK, Maybe you do want to send the hex representation rather than
> the actual data (I can't think why unless you have a very strange
> parser at the other end). In that case I think you do need  to insert
> the \x characters.
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090505/48949c1a/attachment.htm>


More information about the Tutor mailing list