Skip to content
Advertisement

expend valid string in byte array

array x contains string followed by all zeros. I need expand the string but keep x array’s size the same.

e.g.:

JavaScript

expected output:

JavaScript

What’s propery way to do it in python3?

Advertisement

Answer

You can use ljust to add a number of characters to make the resulting bytes the length you want:

JavaScript

So, all in all: strip the trailing zeroes out of the source bytes, add in the bit you want to add, re-pad back to the original length:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement