0

Here is the code snippet:-

join_values = []
new_byteArray = [128, 0, 0, 0, 86, 70, 124, -96]
for values in byteArray:
 values = long(values)
 store_values = struct.pack('!q', values)
 join_values.append(store_values)
print join_values

This produces correct result which is:-

['\x00\x00\x00\x00\x00\x00\x00\x80',
 '\x00\x00\x00\x00\x00\x00\x00\x00', 
 '\x00\x00\x00\x00\x00\x00\x00\x00', 
 '\x00\x00\x00\x00\x00\x00\x00\x00', 
 '\x00\x00\x00\x00\x00\x00\x00V', 
 '\x00\x00\x00\x00\x00\x00\x00F', 
 '\x00\x00\x00\x00\x00\x00\x00|', 
 '\xff\xff\xff\xff\xff\xff\xff\xa0']

This result is correct but isn't there any way I can do a combine pack or do something so that the last out of 8 bytes concatenate together so that I can get the output as:-

\x80\x00\x00x00VF|\xa0
Chad S.
6,67918 silver badges26 bronze badges
asked Oct 12, 2015 at 15:52
1
  • 1
    is there a typo in your desired output? Shouldn't it be \x80\x00\x00\x00\x00V\x00F\x00|\xa0 Commented Oct 12, 2015 at 16:00

1 Answer 1

3

Well, you can get the last byte from a string s by using s[-1].

python2.7 has a feature called list comprehension which can be used to perform a transformation on each element of a list.

Finally, join the strings together.

Putting that all together would look something like

my_transformed_list = [s[-1] for s in join_values]
concatenated = ''.join(my_transformed_list)

Another way to solve this would be with functional programming. The act of performing a transformation on every element of a list is called a map:

my_transformed_list2 = map(lambda s: s[-1], j)
concatenated2 = ''.join(my_transformed_list2)
answered Oct 12, 2015 at 15:59
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.