[Tutor] Concatenating numeric data in Python 3.3

Prasad, Ramit ramit.prasad at jpmorgan.com
Wed Apr 24 20:45:07 CEST 2013


Please post your response *after* the quoted context.

sparkle Plenty wrote:
> 
> Thanks to both of you for your assistance.
> Since the completed message must be in hex, I also have an issue with losing high order zeros during
> conversions, although the binary shift works well for adding on to the end of the string.  I cannot
> pack string data, and I cannot concatenate numbers, so the conversions, although smelly, were what I
> knew to try.  I welcome the idea of abandoning that course of action.  It didn't work anyway.
> 
>  I will have an unknown number of instances, each of which consists of 3 numeric values.  I tried
> packing each instance using struct.pack, then converting to  a tuple, then concatenating.  This ran,
> but introduced errors: in some instances, high order truncation of a leading zero in the first of the
> 3 values.  This throws off the position of all data that follows.  Error messages and error code are
> dependent on which technique is being tried.  The most recent ones are:
> TypeError: can't concat bytes to str
> TypeError: Can't convert 'tuple' object to str implicitly
> 
> 

Can you give us the format and samples of the input and the output? We do not
need real data, often you can just use data of the same type but change values.

If you need hex, you can turn numbers into hex str by using the hex() built-in.

>>> ''.join([hex(15), hex(1239393), hex(49494)])
'0xf0x12e9610xc156' # Need to handle formatting

You may want to also look at the binascii module for some functions for 
converting data. Again, if you provide some sample output (and hopefully input) 
then we can help you better.


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  


More information about the Tutor mailing list