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
1 Answer 1
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)
\x80\x00\x00\x00\x00V\x00F\x00|\xa0